On Fri, Mar 25, 2011 at 1:23 PM, Joey Zhou <yimutang / gmail.com> wrote: > Here is a sample code: > > str = "ABC" > class << str > ¨Â óåìæ > end > > What I get is somthing like "#<Class:#<String:0x15d4bb0>>". > > If "p str", there's an error message: > > x.rb:3:in `singletonclass': undefined local variable or method `str' for > #<Class:#<String:0x1625bf8>> (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 Now if you were looking for a way to get from a singleton class of an object to the object itself, I am not sure that this is possible. I have not yet seen a case where I would have needed it, as I am constructing my singletons myself;). Would you know such a usecase? e.g. an API that passes in a singleton class and where it would be useful to have access to the singlee - I just made this word up, I guess. HTH Robert > > -- > Posted via http://www.ruby-forum.com/. > > -- The 1,000,000th fibonacci number contains '42' 2039 times; that is almost 30 occurrences more than expected (208988 digits). N.B. The 42nd fibonacci number does not contain '1000000' that is almost the expected 3.0e-06 times.