[rspec-devel] ruby1.9 constant lookup rules

Daniel DeLorme dan-ml at dan42.com
Fri Oct 10 10:16:03 EDT 2008


So I was trying to make rspec work with ruby1.9. The first bugs I got 
when running "rake spec" were not difficult to fix:
http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/437

But after that it gets a little more complicated. Constant lookup rules 
have changed in 1.9; basically they are no longer lexically scoped. This 
means an instance_eval changes the lookup path of a given constant. Like 
this:

   def describe(&block)
     instance_eval(&block)
   end
   module Foo
     Bar = 42
     def self.bar
       describe do
         puts Bar
       end
     end
   end
   Foo.bar
   => NameError: uninitialized constant Module::Bar

Because rspec uses instance_eval for the "describe" blocks, this is a 
problem. And it's not really something that a simple patch can fix, this 
is more of a design issue IMHO. I could imagine patching this with some 
const_missing magic, but I think that would be going against the grain 
of ruby1.9. You could also change all constant names to be fully 
qualified (as in the patch I attached to this mail) but that is really ugly.

This blog post by _why seems to have come just in time:
http://hackety.org/2008/10/06/mixingOurWayOutOfInstanceEval.html
That seems like a good alternative to instance_eval; after all we don't 
really want to evaluate the block in a completely different context; we 
just want to have a few extra methods available inside it.

But this is not a decision that a rspec newbie like me can make.

--
Daniel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fully-qualified-constants.patch
Type: text/x-diff
Size: 9502 bytes
Desc: not available
URL: <http://rubyforge.org/pipermail/rspec-devel/attachments/20081010/ff37c6d7/attachment-0001.bin>


More information about the rspec-devel mailing list