David Vollbracht - Hometag:www.davidvollbracht.com,2008:mephisto/Mephisto Noh-Varr2008-07-01T04:00:04Zdvollbrachttag:www.davidvollbracht.com,2008-07-01:422008-07-01T02:48:00Z2008-07-01T04:00:04Z30 Days of Tech: Day 30 - Haskell Decompose<p>Some time ago Tom, a friend, presented a math programming challenge to a me and a few others. The challenge was to create a function that when given a positive number would return a set of all the sets of positive numbers whose sum was the original number. He wasn’t so much interested in the result itself as he was in what sort of techniques were used in the implementation—memoization of course being a choice candidate for this problem. I was toying a lot with Haskell at the time, and since this was fundamentally a math problem, Haskell seemed well suited to the problem too.</p>
<p>Some time ago Tom, a friend, presented a math programming challenge to a me and a few others. The challenge was to create a function that when given a positive number would return a set of all the sets of positive numbers whose sum was the original number. He wasn’t so much interested in the result itself as he was in what sort of techniques were used in the implementation—memoization of course being a choice candidate for this problem. I was toying a lot with Haskell at the time, and since this was fundamentally a math problem, Haskell seemed well suited to the problem too.</p>
<p>Here’s what I came up with:</p>
<pre>
<code>
module Decompose
where
import Data.List
decompose = ((map decompose' [0..]) !!) where
decompose' n = nub (map sort (decompositions n (floor ((toRational n)/2))))
decompositions n 0 = [[n]]
decompositions n m = nmCombinations ++ decompositions n (m - 1) where
nmCombinations = [a ++ b | a <- (decompose (n - m)), b <- (decompose m)]
</code>
</pre>
<p>I was surprised at how short the Haskell solution was. If you run this through Hugs, you’ll see something like this:</p>
<pre>
<code>
Hugs> :load "Decompose"
Decompose> decompose 5
[[1,1,1,1,1],[1,1,1,2],[1,2,2],[1,1,3],[2,3],[1,4],[5]]
Decompose> decompose 10
[[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,2],[1,1,1,1,1,1,2,2],[1,1,1,1,1,1,1,3],[1,1,1,1,1,2,3],[1,1,1,1,1,1,4],[1,1,1,1,1,5],[1,1,1,1,2,2,2],[1,1,1,2,2,3],[1,1,1,1,2,4],[1,1,1,2,5],[1,1,2,2,2,2],[1,2,2,2,3],[1,1,2,2,4],[1,2,2,5],[1,1,1,1,3,3],[1,1,2,3,3],[1,1,1,3,4],[1,1,3,5],[2,2,3,3],[1,2,3,4],[2,3,5],[1,1,4,4],[1,4,5],[5,5],[1,3,3,3],[3,3,4],[2,2,2,2,2],[2,2,2,4],[2,4,4],[1,1,1,1,6],[1,1,2,6],[2,2,6],[1,3,6],[4,6],[1,1,1,7],[1,2,7],[3,7],[1,1,8],[2,8],[1,9],[10]]
</code>
</pre>
<p>The only performance trick in here is indeed memoization, though that may not be obvious at first. It lies in the definition of <code>decompose</code>. The <code>decompose</code> function is really just a list of decomposition results for all positive numbers, indexed by the number being decomposed. Obviously this would be an infinitely long lost, but Haskell has no problem handling that—it just calculates elements of the list on demand. Since decomposing a number always involves decomposing all the numbers below it, the fact that you have to calculate the list out to the length of the original number being decomposed is not a hindrance, it’s actually an advantage!</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-30:402008-06-30T03:07:00Z2008-06-30T04:00:04Z30 Days of Tech: Day 29 - C, Ruby, & Threads, Oh My!<p>When <a href='http://ph7spot.com/'>Philippe</a> and I worked on SystemTimer together we went back and forth about how much to write in Ruby and how much to write in C. Writing purely at the C level gave us more control over what was happening in the Ruby process as we installed and cleared signal handlers and such, but working in Ruby produced far more readable code.</p>
<p>When <a href='http://ph7spot.com/'>Philippe</a> and I worked on SystemTimer together we went back and forth about how much to write in Ruby and how much to write in C. Writing purely at the C level gave us more control over what was happening in the Ruby process as we installed and cleared signal handlers and such, but working in Ruby produced far more readable code.</p>
<p>Ultimately we ended up writing a hybrid—Ruby code that calls C code that calls back into Ruby code for a few interesting bits. When I go back and look at the result now, I’m pretty happy with it. There was one trick we had to pull in our situation, however, to ensure everything went according to plan. Since we entered C code to perform low level operations with timers we felt we didn’t want our code to be interrupted by Ruby’s thread scheduling. We wanted to ensure the timer got set up properly without the chance of another thread being scheduled. At first this led is to try to write as much in C as possible, since we wouldn’t ever hit the Ruby thread scheduler. Eventually we realized we could clean up a bunch of C code by extracting Ruby methods. To prevent other threads from being scheduled, we simply extracted C functions to wrap the Ruby methods, like this one.</p>
<pre>
<code>
static void install_ruby_sigalrm_handler(VALUE self) {
rb_thread_critical = 1;
rb_funcall(self, rb_intern("install_ruby_sigalrm_handler"), 0);
rb_thread_critical = 0;
}
</code>
</pre>
<p>The C function <code>install_ruby_sigalrm_handler</code> is called by another C function, and is quite readable in that context. If you’re reading the C code, you don’t even have to know it’s calling back to Ruby. All the C function does, however, is set <code>rb_thread_critical</code> and then call back into Ruby code. <code>rb_thread_critical</code> is the equivalent of <code>Thread.critical</code> in the Ruby <span class='caps'>API</span>. When it is set, Ruby will not schedule another thread (except in a few cases documented with <code>Thread.critical</code>). Using this simple tool we were able to effectively blend Ruby and C code without worrying about our operations being preempted. Arigatoo, Matsumoto-san.</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-29:392008-06-29T03:08:00Z2008-06-29T04:00:04Z30 Days of Tech: Day 28 - lambda args<p>The Ruby documentation for the <code>lambda</code> method (a.k.a. <code>proc</code>) says “Equivalent to Proc.new, except the resulting Proc objects check the number of parameters passed when called.” This is almost exactly right. There are, however, at least two cases where this turns out to be false.</p>
<p>The Ruby documentation for the <code>lambda</code> method (a.k.a. <code>proc</code>) says “Equivalent to Proc.new, except the resulting Proc objects check the number of parameters passed when called.” This is almost exactly right. There are, however, at least two cases where this turns out to be false.</p>
<p>The first is when the block passed to <code>lambda</code> does not have an argument list specified. The resulting <code>Proc</code> object will not check arguments in this case.</p>
<pre>
<code>
def test_lambda_checks_arguments
l = lambda {||}
assert_raises(ArgumentError) {l.call(1)}
end
def test_lambda_doesnt_check_arguments_if_no_arguments_specified
l = lambda {}
assert_nothing_raised {l.call(1)}
end
</code>
</pre>
<p>The two tests above prove that <code>Proc#call</code> doesn’t check arguments for a <code>lambda</code> proc with no arguments <strong>unless you specify that the proc takes no arguments by including an empty parameter list.</strong> This is not the only case where argument checking is not done for <code>lambda</code> procs, however.</p>
<p>If you pass a <code>lambda</code> proc to a method is a block (using <code>&</code>), this circumvents the argument checking that would normally take place, even when you specify a blank argument list.</p>
<pre>
<code>
def test_lambda_doesnt_check_arguments_when_yielded_to_even_if_arguments_specified
l = lambda {||}
def yield_1_to; yield 1; end
assert_nothing_raised {yield_1_to &l}
end
</code>
</pre>
<p>The above test passes, even though one argument is yielded to a <code>lambda</code> proc that explicitly takes no args. I haven’t read the source yet, but my guess about what’s happening here is that Ruby converts the proc back to a normal block. Since blocks normally don’t check arguments, there’s no reason to expect the one here to either. Nonetheless, the result surprised me until I thought about it longer.</p>
<p>I categorize this as one of those Ruby oddities that for the most part doesn’t hurt you in practice. I don’t recall ever having run into a bug or debugging issue where not knowing about this hindered me. I do think that building knowledge about oddities like these is good practice though. It deepens your knowledge of Ruby in general, and every so often one small bit of knowledge will save you a chunk of time.</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-28:382008-06-28T03:30:00Z2008-06-28T04:31:16Z30 Days of Tech: Day 27 - Short Circuited Timeout<p>Ruby’s <code>timeout</code> method operates by raising a exception, <code>Timeout::Error</code>. <code>Timeout::Error</code> inherits from <code>Interrupt</code>, <code>SignalException</code>, and finally <code>Exception</code>. In particular, it doesn’t inherit from <code>StandardError</code>, which means it isn’t caught by a default <code>rescue</code> clause with no exception class specified. When I first encountered this, I was annoyed that I had to explicitly catch timeout errors anytime I wanted to recover from errors in general, whether they were timeout errors or not. I realized something the other day that changed my opinion on this however.</p>
<p>Ruby’s <code>timeout</code> method operates by raising a exception, <code>Timeout::Error</code>. <code>Timeout::Error</code> inherits from <code>Interrupt</code>, <code>SignalException</code>, and finally <code>Exception</code>. In particular, it doesn’t inherit from <code>StandardError</code>, which means it isn’t caught by a default <code>rescue</code> clause with no exception class specified. When I first encountered this, I was annoyed that I had to explicitly catch timeout errors anytime I wanted to recover from errors in general, whether they were timeout errors or not. I realized something the other day that changed my opinion on this however.</p>
<p>Here’s the <code>timeout</code> method for Ruby 1.8.6.</p>
<pre>
<code>
def timeout(sec, exception=Error)
return yield if sec == nil or sec.zero?
raise ThreadError, "timeout within critical session" if Thread.critical
begin
x = Thread.current
y = Thread.start {
sleep sec
x.raise exception, "execution expired" if x.alive?
}
yield sec
# return true
ensure
y.kill if y and y.alive?
end
end
</code>
</pre>
<p>It operates by starting a thread, <code>y</code> that will raise an exception after sleeping. It raises the exception by calling <code>x.raise</code>, causing the exception be raised in thread <code>x</code>, which is the thread that called <code>timeout</code>—the one doing the work that needs to be timed out. That exception terminates the execution of the thread wherever it happens to be and starts propagating an exception up the stack. This exception is subject to the same handling as every other exception that might happen. It can caught be a rescue clause. <strong>In particular, it can be caught by a rescue clause that is within the block being subjected to the timeout</strong></p>
<pre>
<code>
require 'timeout'
begin
Timeout.timeout(1) do
begin
sleep 2
rescue Exception => e
puts "Caught #{e.inspect} within timeout"
end
end
rescue Exception => e
puts "Caught #{e.inspect} at top level"
end
</code>
</pre>
<p>This produces the output <code>Caught #<Timeout::Error: execution expired> within timeout</code>. If <code>Timeout::Error</code> extended from <code>StandardError</code>, <strong>it would be caught by any generic rescue expression</strong> that would handle exceptions occurring at the point where the timeout error happened to be raised. This rescue clause may not even be in your application code—it could be in a library or the Rails framework, which could result in very hard to track down bugs. We can verify this is the case by specifying what type of error <code>timeout</code> should raise.</p>
<pre>
<code>
require 'timeout'
begin
Timeout.timeout(1, StandardError) do
begin
sleep 2
rescue => e
puts "Caught #{e.inspect} within timeout"
end
end
rescue => e
puts "Caught #{e.inspect} at top level"
end
</code>
</pre>
<p>The only differences here are that <code>Timeout.timeout</code> is passed the type of exception to raise, <code>StandardError</code>, and that the rescue clauses have be changed to omit the exception type. The output is essentially the same as before, <code>Caught #<StandardError: execution expired> within timeout</code>.</p>
<p>Incidentally, <code>SystemTimer</code> terminates the offending thread by raising an exception as well. If there is a rescue clause that handles <code>Timeout::Error</code> within the block given for either <code>Timeout</code> or <code>SystemTimer</code>, it could prevent the error from reaching handling code outside the timeout call. Fortunately the likelihood of encountering this scenario is minimized because <code>Timeout::Error</code> doesn’t inherit from <code>StandardError</code>. I haven’t hit a bug caused by this in practice yet. I’m sure if I had before I had thought through the situation it would have taken me a lot longer to figure out what was going on.</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-27:372008-06-27T03:25:00Z2008-06-27T05:27:58Z30 Days of Tech: Day 26 - Fixnum#object_id<p>In Ruby every object has an object_id that uniquely identifies that object in that particular instance of the Ruby process. Ruby also treats integers as objects—they are instances of <code>Fixnum</code>. For efficiency reasons, though, Ruby implements integers internally as simple values. That is, it doesn’t allocate heap memory for every integer you use, it just passes the value around instead. So how does Ruby generate object ids for all these integers?</p>
<p>In Ruby every object has an object_id that uniquely identifies that object in that particular instance of the Ruby process. Ruby also treats integers as objects—they are instances of <code>Fixnum</code>. For efficiency reasons, though, Ruby implements integers internally as simple values. That is, it doesn’t allocate heap memory for every integer you use, it just passes the value around instead. So how does Ruby generate object ids for all these integers?</p>
<p>You may have never noticed, but all the object ids that you usually end up seeing are even.</p>
<pre>
<code>
irb(main):011:0> Object.object_id
=> 110230
irb(main):012:0> Object.new.object_id
=> 191490
irb(main):013:0> nil.object_id
=> 4
irb(main):014:0> "a_string".object_id
=> 177360
</code>
</pre>
<p>This is because Ruby is reserving all the odd numbers to be object_ids for <code>Fixnums</code>.</p>
<pre>
<code>
irb(main):015:0> 1.object_id
=> 3
irb(main):016:0> 2.object_id
=> 5
irb(main):017:0> 3.object_id
=> 7
irb(main):018:0> 4.object_id
=> 9
</code>
</pre>
<p>Not only are they reserved, they’re calculated using a simple formula! This allows Ruby to easily get back to the value from the id if you should ever ask for it.</p>
<pre>
<code>
irb(main):022:0> ObjectSpace._id2ref(9)
=> 4
</code>
</pre>
<p>And now if you ever see an odd (as in n % 2 = 1, not strange) object_id in some Ruby output somewhere, you’ll know immediately what the value of that object is!</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-26:362008-06-26T01:27:00Z2008-06-26T02:00:04Z30 Days of Tech: Day 25 - Gem dependency version<p>RubyGems and I had a fight. It was about what version of <code>net-ssh</code> we wanted to have installed on my machine. RubyGems wanted the latest (2.0.2) and I wanted a 1.x version (1.1.4). Apparently <code>net-ssh</code> 2 has an issue with sshing into Fedora Core 4, which I needed it to do. Specifically I needed to use capistrano <code>1.4.1</code> to deploy an app to Fedora Core 4</p>
<p>RubyGems and I had a fight. It was about what version of <code>net-ssh</code> we wanted to have installed on my machine. RubyGems wanted the latest (2.0.2) and I wanted a 1.x version (1.1.4). Apparently <code>net-ssh</code> 2 has an issue with sshing into Fedora Core 4, which I needed it to do. Specifically I needed to use capistrano <code>1.4.1</code> to deploy an app to Fedora Core 4</p>
<p>This is something I’ve been doing regularly for some time, but ever since the <a href='http://davidvollbracht.com/2008/6/10/30-days-of-tech-day-9-macbrick'>MacBrick</a> incident I haven’t needed these specific gems installed, so I was starting from scratch. Once I figured out what version of the gems I wanted, here was my first attempt:
(I included <code>--no-rdoc</code> and <code>--no-ri</code> just to save time writing this post).</p>
<pre>
<code>
$ sudo gem install net-ssh --version=1.1.4 --no-rdoc --no-ri
Bulk updating Gem source index for: http://gems.rubyforge.org/
Successfully installed net-ssh-1.1.4
1 gem installed
$ sudo gem install capistrano --version=1.4.1 --no-rdoc --no-ri
Bulk updating Gem source index for: http://gems.rubyforge.org/
Successfully installed net-ssh-2.0.2
Successfully installed net-sftp-2.0.1
Successfully installed capistrano-1.4.1
3 gems installed
</code>
</pre>
<p>Clearly this didn’t work so well. Installing capistrano installed the latest version of net-ssh. But why? Capistrano’s gemspec for 1.4.1 says this</p>
<pre>
<code>
...
s.add_dependency(%q<net-ssh>, [">= 1.0.10"])
s.add_dependency(%q<net-sftp>, [">= 1.1.0"])
...
</code>
</pre>
<p>But <code>net-sftp</code> 2.0.1’s gemspec says it needs <code>net-ssh</code> 1.99.1. So we need to install specific version of both. So, attempt number two.</p>
<pre>
<code>
$ sudo gem install net-ssh --version=1.1.4 --no-rdoc --no-ri
Bulk updating Gem source index for: http://gems.rubyforge.org/
Successfully installed net-ssh-1.1.4
1 gem installed
$ sudo gem install net-sftp --version=1.1.0 --no-rdoc --no-ri
Bulk updating Gem source index for: http://gems.rubyforge.org/
Successfully installed net-ssh-2.0.2
Successfully installed net-sftp-1.1.0
2 gems installed
</code>
</pre>
<p>Rats. Even though <code>net-sftp</code> 1.1.0 only specifies that it depends on <code>net-ssh</code> >= 1.0.0, RubyGems went ahead and installed the latest version anyway. It assumed that even though the dependency was satisfied, I want the latest version of <code>net-ssh</code>— exactly what I’m trying to avoid.</p>
<p>Armed with this information, I made one last ditch attempt.</p>
<pre>
<code>
$ sudo gem install capistrano --version=1.4.1 --no-rdoc --no-ri
Bulk updating Gem source index for: http://gems.rubyforge.org/
Successfully installed net-ssh-2.0.2
Successfully installed net-sftp-2.0.1
Successfully installed capistrano-1.4.1
3 gems installed
$ sudo gem install net-ssh --version=1.1.4 --no-rdoc --no-ri
Bulk updating Gem source index for: http://gems.rubyforge.org/
Successfully installed net-ssh-1.1.4
1 gem installed
$ sudo gem install net-sftp --version=1.1.0 --no-rdoc --no-ri
Bulk updating Gem source index for: http://gems.rubyforge.org/
Successfully installed net-sftp-1.1.0
1 gem installed
$ sudo gem uninstall net-sftp net-ssh
Select gem to uninstall:
1. net-sftp-1.1.0
2. net-sftp-2.0.1
3. All versions
> 2
Successfully uninstalled net-sftp-2.0.1
Select gem to uninstall:
1. net-ssh-1.1.4
2. net-ssh-2.0.2
3. All versions
> 2
Successfully uninstalled net-ssh-2.0.2
</code>
</pre>
<p>Finally, by letting RubyGems install the latest version of <code>net-ssh</code> and <code>net-sftp</code>, then installing the versions I wanted, and the uninstalling the offending versions, I was able to get into a working state to deploy. Hopefully I’ll never run into this problem again. If you should be so unlucky, however, I hope this knowledge will help you.</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-25:352008-06-25T03:18:00Z2008-06-25T04:00:04Z30 Days of Tech: Day 24 - drubyall<p>When we first started trying to use distributed DeepTest, we quickly ran into an issue with DRb servers not binding to the correct addresses. DRb does provide a way to bind to <code>0.0.0.0</code> to listen to all addresses, but there’s a downside to the way it does it. The url you provide to DRb to accomplish this must omit the hostname (e.g. <code>druby://:0/</code>). DRb then determines what hostname to report to clients by calling <code>getaddrinfo</code> with the empty hostname. Unfortunately for us <code>getaddrinfo</code> didn’t provide a hostname that could be used by our developer machines to connect to the DeepTest servers. For a brief moment we thought we were stuck.</p>
<p>When we first started trying to use distributed DeepTest, we quickly ran into an issue with DRb servers not binding to the correct addresses. DRb does provide a way to bind to <code>0.0.0.0</code> to listen to all addresses, but there’s a downside to the way it does it. The url you provide to DRb to accomplish this must omit the hostname (e.g. <code>druby://:0/</code>). DRb then determines what hostname to report to clients by calling <code>getaddrinfo</code> with the empty hostname. Unfortunately for us <code>getaddrinfo</code> didn’t provide a hostname that could be used by our developer machines to connect to the DeepTest servers. For a brief moment we thought we were stuck.</p>
<p>When we looked into how DRb handles protocols, however, we realized we could simply add a protocol that would accept a hostname as part of the url and use it to report the DRb url to clients when needed. For listening on the network, however, the protocol always binds to <code>0.0.0.0</code>. By registering this protocol (which we called drubyall since it listens on all interfaces) you can use it in any DRb url, which is pretty handy. Although it wasn’t nice that we ran into the problem, it was very nice that DRb gave us an easy out to extending its communications layer.</p>
<pre>
<code>
module DeepTest
class DRbBindAllTCPSocket < DRb::DRbTCPSocket
def self.parse_uri(uri)
if uri =~ /^drubyall:\/\/(.*?):(\d+)(\?(.*))?$/
host = $1
port = $2.to_i
option = $4
[host, port, option]
else
raise(DRb::DRbBadScheme, uri) unless uri =~ /^drubyall:/
raise(DRb::DRbBadURI, 'can\'t parse uri:' + uri)
end
end
# Open a server listening for connections at +uri+ using
# configuration +config+.
def self.open_server(uri, config)
uri = 'drubyall://:0' unless uri
host, port, opt = parse_uri(uri)
if host.size == 0
host = getservername
end
DeepTest.logger.debug("Listening on port #{port}, all addresses.")
soc = TCPServer.open('0.0.0.0', port)
port = soc.addr[1] if port == 0
uri = "druby://#{host}:#{port}"
self.new(uri, soc, config)
end
end
end
DRb::DRbProtocol.add_protocol DeepTest::DRbBindAllTCPSocket
</code>
</pre>
dvollbrachttag:www.davidvollbracht.com,2008-06-24:342008-06-24T02:11:00Z2008-06-24T03:00:04Z30 Days of Tech: Day 23 - DearSoul
<pre>
<code>
class DearSoul
def self.new(first, last)
ObjectSpace.define_finalizer(allocate, proc {|id|
puts "Goodbye, #{first}. We'll miss you."
})
end
end
DearSoul.new("George", "Carlin")
</code>
</pre>
dvollbrachttag:www.davidvollbracht.com,2008-06-23:332008-06-23T02:58:00Z2008-06-23T04:00:04Z30 Days of Tech: Day 22 - Class#to_proc
<p>Here’s a little hack for Ruby classes that comes from a project.</p>
<pre>
<code>
class Class
def to_proc
method(:new).to_proc
end
end
</code>
</pre>
<p>If you’ve ever used <code>map</code> (a.k.a. <code>collect</code>) to create a bunch of objects you might be familiar with code that looks something like this.</p>
<pre>
<code>
value_list.map {|v| Something.new(v)}
</code>
</pre>
<p>With <code>Class#to_proc</code> this can be simplified to</p>
<pre>
<code>
value_list.map(&Something)
</code>
</pre>
<p>This works very well for <code>map</code>, but I haven’t seen <code>Class#to_proc</code> become useful in any other situation to date. Even changing <code>map</code> to <code>collect</code> changes my opinion of it’s expressiveness. I don’t know that <code>Class#to_proc</code> is pulls its weight in our situation, but I just like the <code>map</code> statement above too much to give it up.</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-22:322008-06-22T03:33:00Z2008-06-22T05:00:03Z30 Days of Tech: Day 21 - Object Literals<p>You can do object oriented programming in both Ruby and Javascript. I think most people would agree that Ruby is “more” object oriented than Javascript, whatever subjective judgement they’re using. One thing that’s interesting to me, though, is that Javascript has a syntax for literal objects built in where as Ruby does not.</p>
<p>You can do object oriented programming in both Ruby and Javascript. I think most people would agree that Ruby is “more” object oriented than Javascript, whatever subjective judgement they’re using. One thing that’s interesting to me, though, is that Javascript has a syntax for literal objects built in where as Ruby does not.</p>
<p>Javascript is prototype based, which means that every object that’s created actually just copied from another object. The object being copied is called the “prototype”, and forms a sort of factory for creating new objects. But then were do the prototype objects come from? If <strong>all</strong> objects are created from prototypes, shouldn’t the prototypes be created from other prototypes? Javascript sort of sidesteps this issue a bit—it’s only half prototype based. All objects in Javascript are really just hashes and Javascript has a syntax for writing hash literals. That means you can create an object in Javascript just by creating a hash and skip the whole prototype business.</p>
<pre>
<code>
var counter = {
count: 0,
increment: function() {this.count +=1},
decrement: function() {this.count -=1}
};
</code>
</pre>
<p>This is a perfectly good counter object in Javascript. If you wanted to create a similar object in Ruby, the most straightforward way would be simply to create a class.</p>
<pre>
<code>
class Counter
def initialize
@count = 0
end
def increment
@count += 1
end
def decrement
@count -= 1
end
end
counter = Counter.new
</code>
</pre>
<p>There’s nothing wrong with this implementation at all. Every so often, though, I run into a situation where I just want to roll an object with 1 or 2 methods inline rather that creating a class. The class usually ends up outside the method, at best, if not in a separate file. In general this is good separation, but if the code is very simple it is sometimes more clear to simply see it in the context of the method that needs it. There is a roundabout way of achieving this in Ruby.</p>
<pre>
<code>
counter = Object.new.instance_eval do
@count = 0
def increment
@count += 1
end
def decrement
@count -= 1
end
self
end
</code>
</pre>
<p>I don’t think this buys you very much. It’s not very intention revealing, which means a new person reading it may spend as long understanding why it’s written like this as they would looking at up a separate class. Wouldn’t it be nice if Ruby supported a syntax to clean this up at least a bit? Maybe something like</p>
<pre>
<code>
object counter
@count = 0
def increment
@count += 1
end
def decrement
@count -= 1
end
end
</code>
</pre>
<p>Well, we can’t quite make it there without extending the syntax, but we can make a reasonable facsimile.</p>
<pre>
<code>
def object(&block)
object = Object.new
object.instance_eval(&block)
object
end
counter = object do
@count = 0
def increment
@count += 1
end
def decrement
@count -= 1
end
end
</code>
</pre>
<p>If you find yourself working in a system that demands a lot of object literals for some reason, I’m sure you’ll want something of the sort. Also, if you’re working on such a system, I fear for your soul…</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-21:292008-06-21T03:36:00Z2008-06-21T05:00:04Z30 Days of Tech: Day 20 - Unmixed Methods
<p><a href='http://rubyforge.org/projects/mixology/'>mixology</a> is a Ruby extension implemented by some of the Something Nimble collaborators. In short, it allows you to add modules to Objects (much like Ruby’s <code>extend</code>), but in such a way that they can also be removed. Mixology refers to these operations as <code>mixin</code> and <code>unmix</code>. If you want to know more about Mixology and the people behind it, <a href='http://www.somethingnimble.com/bliki/mixology'>here is a good resource</a>. For the purposes of our discussion, here’s a simple example:</p>
<pre>
<code>
irb(main):001:0> require 'rubygems'; require 'mixology'
=> true
irb(main):002:0> module A
irb(main):003:1> def foo; puts "bar"; end
irb(main):004:1> end
=> nil
irb(main):005:0> o = Object.new
=> #<Object:0x4f2b8>
irb(main):006:0> o.foo
NoMethodError: undefined method `foo' for #<Object:0x4f2b8>
from (irb):6
irb(main):007:0> o.mixin A
=> #<Object:0x4f2b8>
irb(main):008:0> o.foo
bar
=> nil
irb(main):009:0> o.unmix A
=> #<Object:0x4f2b8>
irb(main):010:0> o.foo
NoMethodError: undefined method `foo' for #<Object:0x4f2b8>
from (irb):10
</code>
</pre>
<p>We can see that after <code>o.unmix A</code> the <code>foo</code> method is no longer accessible on the object <code>o</code>. But is there any way we could end up executing the <code>foo</code> method anyhow? It turns out there is. Let’s look at this example:</p>
<pre>
<code>
irb(main):001:0> require 'rubygems'; require 'mixology'
=> true
irb(main):002:0> module A
irb(main):003:1> def foo; puts self.inspect; end
irb(main):004:1> end
=> nil
irb(main):005:0> o = Object.new
=> #<Object:0x4d1ac>
irb(main):006:0> o.mixin A
=> #<Object:0x4d1ac>
irb(main):007:0> o.foo
#<Object:0x4d1ac>
=> nil
irb(main):008:0> foo = o.method(:foo)
=> #<Method: Object(A)#foo>
irb(main):009:0> o.unmix(A)
=> #<Object:0x4d1ac>
irb(main):010:0> o.foo
NoMethodError: undefined method `foo' for #<Object:0x4d1ac>
from (irb):10
irb(main):011:0> foo.call
#<Object:0x4d1ac>
=> nil
</code>
</pre>
<p>In this second example we can see that even though <code>foo</code> can’t be invoked through the object (since module <code>A</code> is no longer mixed in), Ruby has no problem invoking the method using the <code>Method</code> object that was grabbed before unmixing <code>A</code>. So yes, we can access <code>foo</code> if we should absolutely need to. There is one gotcha with this technique, however—even though we are in <code>foo</code>, no other methods from <code>A</code> are available!</p>
<pre>
<code>
irb(main):001:0> require 'rubygems'; require 'mixology'
=> true
irb(main):002:0> module A
irb(main):003:1> def called_as_method_object
irb(main):004:2> puts "got in, executing method_no_longer_available"
irb(main):005:2> method_no_longer_available
irb(main):006:2> end
irb(main):007:1>
irb(main):008:1* def method_no_longer_available
irb(main):009:2> raise "Will never make it"
irb(main):010:2> end
irb(main):011:1> end
=> nil
irb(main):012:0> o = Object.new
=> #<Object:0x366dc>
irb(main):013:0> o.mixin A
=> #<Object:0x366dc>
irb(main):014:0> method = o.method(:called_as_method_object)
=> #<Method: Object(A)#called_as_method_object>
irb(main):015:0> o.unmix A
=> #<Object:0x366dc>
irb(main):016:0> method.call
got in, executing method_no_longer_available
NameError: undefined local variable or method `method_no_longer_available' for #<Object:0x366dc>
from (irb):5:in `called_as_method_object'
from (irb):16:in `call'
from (irb):16
from :0
</code>
</pre>
<p>The implication of this is significant. If the code in <code>called_as_method_object</code> becomes complicated, you can’t even perform extract method to refactor it! Given that this technique is obscure to begin with <strong>and</strong> has this major limitation, I recommend you not use it if at all possible. That being said, you may find yourself in a situation where it’s the best option available, at least for the moment. I mean, how else would I come up with this stuff?</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-20:282008-06-20T03:58:00Z2008-06-20T06:00:56Z30 Days of Tech: Day 19 - instance_eval & Method#to_proc<p>Ruby has <code>instance_eval</code> that accepts a block and executes it with <code>self</code> set to the receiver of <code>instance_eval</code>. Ruby also has a method called <code>method</code> that returns a <code>Method</code> object representing a callable method on an object. <code>Method</code> objects, in turn, have a method called <code>to_proc</code> which allows them to be converted to blocks. These are all facts I knew when the day began, but never before had this question occurred to me: what happens when you pass the result of <code>Method#to_proc</code> into <code>instance_eval</code>? What is <code>self</code>? The answer surprised me actually…</p>
<p>Ruby has <code>instance_eval</code> that accepts a block and executes it with <code>self</code> set to the receiver of <code>instance_eval</code>. Ruby also has a method called <code>method</code> that returns a <code>Method</code> object representing a callable method on an object. <code>Method</code> objects, in turn, have a method called <code>to_proc</code> which allows them to be converted to blocks. These are all facts I knew when the day began, but never before had this question occurred to me: what happens when you pass the result of <code>Method#to_proc</code> into <code>instance_eval</code>? What is <code>self</code>? The answer surprised me actually…</p>
<p>Ruby has <code>instance_eval</code> that accepts a block and executes it with <code>self</code> set to the receiver of <code>instance_eval</code>. Ruby also has a method called <code>method</code> that returns a <code>Method</code> object representing a callable method on an object. <code>Method</code> objects, in turn, have a method called <code>to_proc</code> which allows them to be converted to blocks. These are all facts I knew when the day began, but never before had this question occurred to me: what happens when you pass the result of <code>Method#to_proc</code> into <code>instance_eval</code>? What is <code>self</code>? The answer surprised me actually…</p>
<pre>
<code>
def test_method_must_take_one_argument_for_instance_eval
instance = Class.new {def take_no_args; end}.new
begin
Object.new.instance_eval(&instance.method(:take_no_args))
flunk "No ArgumentError was raised"
rescue ArgumentError => e
assert_equal "wrong number of arguments (1 for 0)", e.message
end
end
def test_argument_to_method_for_instance_eval_is_receiver_of_instance_eval
instance = Class.new {def return_argument(o); o; end}.new
obj = Object.new
assert_equal obj, obj.instance_eval(&instance.method(:return_argument))
end
def test_self_within_instance_eval_is_original_instance
instance = Class.new {def return_self(o); self; end}.new
assert_equal instance, Object.new.instance_eval(&instance.method(:return_self))
end
</code>
</pre>
<p>The first test was the one that surprised me. An <code>ArgumentError</code> being raised would certainly not have been my first guess about what was going to happen. The fact that the error is raised, however, is due to the answer to the “what is <code>self</code>?” question. It turns out that <code>self</code> is left as the object that the method is from—i.e. the block isn’t really evaluated in the context of the instance that received <code>instance_eval</code>. This seems reasonable, since methods must be executed on instances of the classes they are defined for. Allowing <code>instance_eval</code> to change <code>self</code> for a method would break this rule. What is interesting, however, is that a sort compromise has been struck. Instead of setting <code>self</code> to the receiver of <code>instance_eval</code>, the receiver is <strong>passed to the method as an argument</strong>. Thus if you really need to pass a method object off to some code that is doing <code>instance_eval</code>, you’ll still have access to the object that received the <code>instance_eval</code> message, just in case you need it. I haven’t ever hit this in practice, so I’m not sure whether I’d rather have access to the object or not in general, but I can understand why one might. If you find yourself butting up against this scenario, however, I would encourage you to examine why you’re using <code>Method#to_proc</code> and <code>instance_eval</code> together in the first place.</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-19:272008-06-19T03:56:00Z2008-06-19T05:22:23Z30 Days of Tech: Day 18 - Ajax Defined JS<p>If you’re using prototype.js for Ajax and updating your page with <span class='caps'>HTML</span> that defines new javascript functions, you’re likely to encounter issues. One solution is to pull the function definitions out of the <span class='caps'>HTML</span> to a javascript file that is loaded when the page first loads. More often than not this is the correct solution. However, understanding the problem is quite instructive and leads to another solution that can be used in a pinch when pulling the function out is not an option. ...</p>
<p>If you’re using prototype.js for Ajax and updating your page with <span class='caps'>HTML</span> that defines new javascript functions, you’re likely to encounter issues. One solution is to pull the function definitions out of the <span class='caps'>HTML</span> to a javascript file that is loaded when the page first loads. More often than not this is the correct solution. However, understanding the problem is quite instructive and leads to another solution that can be used in a pinch when pulling the function out is not an option. ...</p>
<p>If you’re using prototype.js for Ajax and updating your page with <span class='caps'>HTML</span> that defines new javascript functions, you’re likely to encounter issues. One solution is to pull the function definitions out of the <span class='caps'>HTML</span> to a javascript file that is loaded when the page first loads. More often than not this is the correct solution. However, understanding the problem is quite instructive and leads to another solution that can be used in a pinch when pulling the function out is not an option.</p>
<p>To set the stage, let’s suppose we have a Rails application with a view that has a button. When that button is clicked, an Ajax request is made that results in another button being added to the page. The new button, in turn, triggers a call to a javascript function that happens to also be defined in the response to the Ajax request. Let’s see what happens…</p>
index.rhtml:
<pre>
<code>
<html>
<head>
<%= javascript_include_tag :defaults %>
</head>
<body>
<h1>Click the Button!</h1>
<button onclick="<%= remote_function :url => {:action => 'new_button'},
:update => 'new_button_target' %>">
Get New Button
</button>
<div id="new_button_target"></div>
</body>
</html>
</code>
</pre>
new_button.rhtml:
<pre>
<code>
<h2>Here's a new button!</h2>
<script type="text/javascript">
function prove_function_defined() {
alert("You called?");
}
</script>
<button onclick="prove_function_defined()">Call Function</button>
</code>
</pre>
<p>Loading the index page in Safari we see what we expect:</p>
<p><img src='http://davidvollbracht.com/assets/2008/6/19/get_new_button.png' alt='' /></p>
<p>Clicking the button seems to work fine too:</p>
<p><img src='http://davidvollbracht.com/assets/2008/6/19/call_function.png' alt='' /></p>
<p>But when we click the new button we get no alert. Instead we get this:</p>
<p><img src='http://davidvollbracht.com/assets/2008/6/19/cant_find_variable.png' alt='' /></p>
<p>Usually the first reaction here is to assume that the javascript in the Ajax response is never evaluated. Unfortunately the problem isn’t so simple. We can prove the javascript is evaluated by adding a simple alert.</p>
<pre>
<code>
...
<script type="text/javascript">
alert("Really, I'm getting defined!");
function prove_function_defined() {
alert("You called?");
}
</script>
...
</code>
</pre>
<p>Now when we click the “Get New Button” button an alert is shown:</p>
<p><img src='http://davidvollbracht.com/assets/2008/6/19/getting_defined.png' alt='' /></p>
<p>The function definition directly follows the alert that shows up, so the function must be getting defined. If so, where is it? We already know it isn’t directly accessible from the onclick on our button. If we dig far enough down into prototype.js, we find that the answer lies in how the javascript from the Ajax response is evaluated.</p>
<pre>
<code>
Element.Methods = {
...
update: function(element, content) {
...
element.innerHTML = content.stripScripts();
content.evalScripts.bind(content).defer();
...
},
..
};
</code>
</pre>
<p>The update function here is the function that prototype calls to update the <code>new_button_target</code> div in index.rhtml. I’ve eliminated the irrelevant parts here to highlight the two lines that matter. Prototype strips out the script tags before setting the <code>innerHTML</code> property of the element. It then sets a timeout (the call to <code>defer</code>) causing the browser to invoke the <code>evalScripts</code> function a little later and continues on it’s merry way. So to understand where our function is being defined, we need to look at <code>evalScripts</code>.</p>
<pre>
<code>
evalScripts: function() {
return this.extractScripts().map(function(script) { return eval(script) });
}
</code>
</pre>
<code>evalScripts</code> extracts the script tags from the html fragment and then calls <code>eval</code> on each one of them. Now we finally come to the crux of it—<code>eval</code> evaluates the script passed to it in the current context, which is within the <code>evalScripts</code> function. Unfortunately for us, javascript allows functions to be defined as part of the local scope of a another function, much like local variables. So our function is being defined, but local to a particular execution of <code>evalScripts</code>. As soon as <code>evalScripts</code> returns, our function is lost. Oh no!
<p>This is where we would probably decide to just pull the function out of the <span class='caps'>HTML</span> being rendered for the Ajax response and make sure it’s loaded when index.rhtml was loaded, but we might run into a situation where this is highly inconvenient. If we find ourselves in such a situation, we can use our knowledge of javascript functions, variables, and scopes to worm our way out. In javascript functions are nothing more than variables that hold function objects. In fact, the syntax for defining a function in javascript is little more than syntactic sugar. We could just as easily assign a variable to a function object ourselves. Furthermore, assignments within a function in javascript create a <strong>global</strong> variable if you do not include the var keyword, which would make it local. Therefore we can force our function to be defined globally simply by changing the code like this.</p>
<pre>
<code>
...
<script type="text/javascript">
prove_function_defined = function() {
alert("You called?");
}
</script>
...
</code>
</pre>
<p>Now when we reload the index page, click on the first button and then the second we get the behavior we want!</p>
<p><img src='http://davidvollbracht.com/assets/2008/6/19/you_called.png' alt='' /></p>
<p>Whew… all that javascript hurts my head. I’m going to go lay down for awhile. Have a good night!</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-18:262008-06-18T03:12:00Z2008-06-18T04:00:04Z30 Days of Tech: Day 17 - Nesting<p>ruby-doc.org describes <code>Module.nesting</code> as “the list of Modules nested at the point of call.” What does this really mean? ...</p>
<p>ruby-doc.org describes <code>Module.nesting</code> as “the list of Modules nested at the point of call.” What does this really mean? ...</p>
<p>ruby-doc.org describes <code>Module.nesting</code> as “the list of Modules nested at the point of call.” What does this really mean?</p>
<pre>
<code>
$top_level_nesting = Module.nesting
module M1
$level_1_nesting = Module.nesting
module M2
$level_2_nesting = Module.nesting
end
end
module M1::M2
$re_opened_nesting = Module.nesting
end
global_variables.select {|v| v =~ /nesting/}.sort.each do |v|
puts "#{v}: #{eval(v).inspect}"
end
</code>
</pre>
<p>Can you guess what this script outputs?</p>
<pre>
<code>
$level_1_nesting: [M1]
$level_2_nesting: [M1::M2, M1]
$re_opened_nesting: [M1::M2]
$top_level_nesting: []
</code>
</pre>
In most of the cases it is straightforward, but <code>$re_opened_nesting</code> might surprise you. You’ve reopened the <code>M2</code> module, which is was defined within <code>M1</code>, but the nesting doesn’t reflect this relationship. The reason is that the module was opened using <code>M1::M2</code> as the module name, rather than reopening <code>M1</code> and then reopening <code>M2</code> from within <code>M1</code>. This point is perhaps more clear looking at this example.
<pre>
<code>
module M1
module M2
end
end
M3 = M1::M2
M3.name #=> "M1::M2"
module M3
Module.nesting #=> [M1::M2]
end
</code>
</pre>
<p>In this case the module <code>M1::M2</code> is assigned to a new constant <code>M3</code> at the top level. This assignment does not change the identify of the module – the name is still <code>M1::M2</code>. When the module is reopened via the <code>M3</code> constant, it’s more clear why there is only one entry in <code>Module.nesting</code>. When a module is opened using the <code>module</code> keyword, Ruby adds it to the current nesting list. It doesn’t matter whether the name that was originally used to define the module was nested under another module or not – it’s the use of the <code>module</code> keyword that causes the nesting to change.</p>
<p>Most of the time you won’t care about what <code>Module.nesting</code> is, but there is a case that really matters if you re-open classes and modules to add extensions or monkey patch code. When Ruby looks up the value of a constant, it uses the nesting to determine which constant to use.</p>
<pre>
<code>
A = 1
module M1
A = 2
module M2
puts "A at #{Module.nesting.inspect}: #{A}"
end
end
module M1::M2
puts "A at #{Module.nesting.inspect}: #{A}"
end
</code>
</pre>
<p>The output of the snippet above is</p>
<pre>
<code>
A at [M1::M2, M1]: 2
A at [M1::M2]: 1
</code>
</pre>
<p>You can see that there are two different constants called <code>A</code>, one nested inside <code>M1</code> and one at the top level. When <code>M1::M2</code> is reopened, one might expect the reference to <code>A</code> to evaluate to the value defined within <code>M1</code>, but instead the top level constant is used. If you ever encounter any unexpected effects dealing with constants when you’re reopening classes nested within modules, keep <code>Module.nesting</code> in mind and it may help you figure out what is going on.</p>
dvollbrachttag:www.davidvollbracht.com,2008-06-17:252008-06-17T03:10:00Z2008-06-17T04:00:04Z30 Days of Tech: Day 16 - Line Animation<p>Here’s a little something pulled out of one of my personal projects. It’s a class to help with single line animations on the command line. You construct it with an output device (something supporting <<, like <span class='caps'>STDOUT</span>) and a list of objects (components). When you tell it to draw, it blanks out the current line and then draws it by converting the list of components to a string and writing it to the display. ...</p>
<p>Here’s a little something pulled out of one of my personal projects. It’s a class to help with single line animations on the command line. You construct it with an output device (something supporting <<, like <span class='caps'>STDOUT</span>) and a list of objects (components). When you tell it to draw, it blanks out the current line and then draws it by converting the list of components to a string and writing it to the display. ...</p>
<p>Here’s a little something pulled out of one of my personal projects. It’s a class to help with single line animations on the command line. You construct it with an output device (something supporting <<, like <span class='caps'>STDOUT</span>) and a list of objects (components). When you tell it to draw, it blanks out the current line and then draws it by converting the list of components to a string and writing it to the display.</p>
<pre>
<code>
class LineDisplay
def initialize(out, components)
@out, @components = out, components
end
def draw
@out << "\r" << " " * @current_length << "\r" if @current_length
string = @components.join
@current_length = string.length
@out << string
@out.flush
end
end
</code>
</pre>
<p>If you combine altering the components that the line display holds references to with calling draw repeatedly from a loop, you can perform a nice little animation, such as this</p>
<pre>
<code>
class Spinner
STATES = ["|", "/", "-", "\\"]
def initialize(state, direction)
@state, @direction = state, direction
end
def step
@state = (@state + (1 * @direction)) % STATES.length
end
def to_s
STATES[@state]
end
end
components = []
0.upto(3) do |starting_state|
components << " "
components << Spinner.new(starting_state, 1)
end
components << " Weee!!!!"
3.downto(0) do |starting_state|
components << " "
components << Spinner.new(starting_state, -1)
end
spinners = components.select {|o| Spinner === o}
display = LineDisplay.new(STDOUT, components)
loop do
display.draw
sleep 0.15
spinners.each {|s| s.step}
end
</code>
</pre>
<p>The last 5 lines of the code above are where the real magic happens. <code>display.draw</code> redraws the line, <code>sleep</code> pauses to control the animation framerate, and <code>spinners.each {|s| s.step}</code> makes the spinners spin! Put it all together and it looks something like this</p>
<pre>
<code>
- \ | / Weee!!!! / | \ -
</code>
</pre>
<p>but more, well, animated. Try it for yourself!</p>