[rspec-devel] ruby1.9 constant lookup rules

David Chelimsky dchelimsky at gmail.com
Fri Oct 10 11:37:11 EDT 2008


On Fri, Oct 10, 2008 at 9:16 AM, Daniel DeLorme <dan-ml at dan42.com> wrote:
> 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.

Hey Daniel,

Thanks for the effort. Would you kindly post this to
http://rspec.lighthouseapp.com?

Cheers,
David

>
> --
> Daniel
>
> _______________________________________________
> rspec-devel mailing list
> rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel
>


More information about the rspec-devel mailing list