From: James Edward Gray II Date: 2007-11-27T04:33:16+09:00 Subject: Re: module_eval is an instance method? On Nov 25, 2007, at 11:43 PM, 7stud -- wrote: > Paul McMahon wrote: >> module_eval is a instance method of Module. >> >> It is available to anything that is a Module. >> >> irb(main):001:0> Object.is_a?(Module) >> => true >> irb(main):002:0> String.is_a?(Module) >> => true >> irb(main):003:0> self.is_a?(Module) >> => false > > Ok, how do you explain this output: > > def meth1 > Object.module_eval('num') > end > > num = 10 > Object.module_eval('num') > -->10 > > meth1 > --> > `module_eval': undefined local variable or method `num' for > Object:Class > (NameError) > > According to pickaxe2, p. 393: > > "When we define methods, we're actually creating (private) instance > methods for class Object." Ruby pretty much says exactly what's going here: num is not a local variable or a method *accessible inside the scope of meth1.* > What about top level variables? I don't reccomend this, but you can get at them: #!/usr/bin/env ruby -wKU def print_num puts eval("num", TOPLEVEL_BINDING) end num = 10 print_num __END__ James Edward Gray II