15 Feb 2013, 13:51
Generic-user-small

Kedar Mhaswade (21 posts)

On p356 (PDF, v5.0), on the section on Singletons, book introduces this snippet:

animal = "cat" 
def animal.speak
  puts "The #{self} says miaow" 
end

and then on p357 it says:

Ruby makes this singleton class the class of the "cat" 
object and makes String (which was the
original class of "cat") the superclass of the singleton 
class.

But I find that animal.class is still String and not the anonymous class. How do I access the anon class that defines this method speak?

27 Feb 2013, 15:38
Dave_8_trans_72dpi_pragsmall

Dave Thomas (233 posts)

Ruby hides the singleton class from you when you ask an object for it’s class. But you can get it using animal.singleton_class

  You must be logged in to comment