Perplexing constant scope behavior

August 18, 2007 Pivotal Labs

Why can’t I refer to constants nested inside a class within a module eval?

irb(main):001:0> class A
irb(main):002:1>    class B
irb(main):003:2>    end
irb(main):004:1> end
=> nil
irb(main):005:0> A.module_eval { puts B }
NameError: uninitialized constant B
           from (irb):5
           from (irb):5:in `module_eval'
           from (irb):5
           from :0

This is frustrating. Can anyone explain why it has to be this way? Perhaps there’s a good reason I’m not considering.

About the Author

Biography

More Content by Pivotal Labs
Previous
How I Learned to Stop Hating and Love Action Mailer
How I Learned to Stop Hating and Love Action Mailer

My biggest gripe with ActionMailer is how difficult it is to generate URL's. It's common enough when sendin...

Next
Taking a break from Rspec
Taking a break from Rspec

On my next new project, I think I'm going back to Test::Unit. I've lost patience with Rspec, and it seems l...

Enter curious. Exit smarter.

Learn More