From: 7stud -- Date: 2011-03-26T08:31:38+09:00 Subject: Re: How to get the value of a singleton class? Robert Dober wrote in post #989182: > On Fri, Mar 25, 2011 at 1:23 PM, Joey Zhou wrote: >> >> x.rb:3:in `singletonclass': undefined local variable or method `str' for >> #> (NameError) >> >> How can I get the value of str("ABC") within the class << str;...;end > > your problem is that class << x...end is *not* a closure > > x= 42 > class << whatever # I disassociate x and whatever for the general case > x # No Method Error as we are *not* in a closure here > end > > But there is a workaround, an idiom you will find quite often in Ruby > metaprogramming > > x=42 > class << whatever; self end.module_eval do > p x # Works like charm as this *is* a closure > end > ...but that is a ridiculous construct. You are creating a block for module_eval() in order to see the local variable x. Well guess what? You don't need a block to see x: x = 42 class << whatever self end puts x -- Posted via http://www.ruby-forum.com/.