From: Alexandre Mutel Date: 2009-12-01T23:53:49+09:00 Subject: Ruby should support consistent constant lookup (and dynamic) Just a general remark about this problem. Shouldn't Ruby have a builtin mechanism to resolve dynamically and efficiently constants handling lexical scopes correctly? Seems that in 1.9, we have this features: irb(main):001:0> class A irb(main):002:1> BAR = 1 irb(main):003:1> class B irb(main):004:2> def self.foo(&b) irb(main):005:3> instance_eval(&b) irb(main):006:3> end irb(main):007:2> end irb(main):008:1> end => nil irb(main):009:0> A::B.foo do irb(main):010:1* puts BAR irb(main):011:1> end 1 => nil irb(main):012:0> BUT now, try to do it with const_get, and we got a NameError: irb(main):012:0> A::B.foo do irb(main):013:1* puts const_get(:BAR) irb(main):014:1> end NameError: uninitialized constant A::B::BAR from (irb):13:in `const_get' from (irb):13:in `block in irb_binding' from (irb):5:in `instance_eval' from (irb):5:in `foo' from (irb):12 from E:/Code/Ruby/bin/irb:12:in `
' This is quite frustrating when writing a DSL language to not being able to resolve constants dynamically (without getting a costly module hierarchy and checking the constant in each module...) Should'nt this constant lookup be consistent between static and const_get? -- Posted via http://www.ruby-forum.com/.