On Thu, Mar 7, 2013 at 12:26 PM, Joel Pearson <[email protected]> wrote:
> Can this work for constants as well? I have a several constants within a
> long class name and I was wondering whether I can use this approach, but
> it doesn't seem to be working for me:
>
> irb(main):001:0> class A
> irb(main):002:1> B = 1
> irb(main):003:1> end
> => 1
> irb(main):004:0> A::B
> => 1
> irb(main):005:0> A.new.instance_eval do
> irb(main):006:1* puts B
> irb(main):007:1> end
> NameError: uninitialized constant B
>         from (irb):6:in `block in irb_binding'
>         from (irb):5:in `instance_eval'
>         from (irb):5
>         from C:/Ruby193/bin/irb:12:in `<main>'

Const lookup works differently from other identifiers (IIRC it's done
via lexical scope).  Having said that you can do

irb(main):007:0> A.new.instance_eval { self.class.const_get :B }
=> 1

Well, probably not what you wanted. :-)

How do you want to use that feature (if it existed)?  Maybe there's
another elegant solution.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- 
[email protected] | 
https://groups.google.com/d/forum/ruby-talk-google?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"ruby-talk-google" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to