small medium large xlarge

15 Feb 2013, 13:51
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"

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 Thomas (366 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