From: "Jan E." Date: 2012-04-17T08:32:13+09:00 Subject: Re: A Beginner's Question about Metaprogramming Phil Stone wrote in post #1056788: > Thanks for all the help, but there are some more question that I have: > > 1. Why is it necessary to use a Singleton class in the above example at > all? An object has two sources for its methods: First of all, it can use the methods defined in its class. Those methods are shared by *all* instances of the class. However, an object can also have methods on its own, which are *not* shared with the other instances. Those methods are called "singleton methods" and are defined in the singleton class of the object (I wouldn't use the word "metaclass", because it can also have a different meaning). The difference between normal methods and singleton methods gets a bit tricky when dealing with classes, because now you basically have to distinguish between three cases: a) A class is a normal object and can use the methods defined in its class. The class of a class is always Class. This means: The methods defined in Class (like "attr_accessor" or "superclass") are shared by all classes. b) Like any other object, a class can also have singleton methods. Those methods only belong to the class itself and not to the other classes. c) A class can also *define* methods for its instances. It this case, the class isn't the user but rather the provider. Now to your example: Where do you want the methods to go? You certainly don't want *all* classes to have a "charisma" method. Instead, you want a *specific* class to have it, namely Creature. That's why you define a singleton method for Creature (case b). However, letting the class set the standard values for "charisma", "strength" etc. doesn't make sense if the instances of Creature don't use these properties. That is, you also have to define correspoding methods for the instances of Creature (case c). This can of course be done automatically by calling "attr_accessor". You may also come to the conclusion that *every* class should have a "traits" method. Then you would define this method in Class (case a). It hope this makes things a bit clearer. > Why are the methods defined onto the Singleton class? I would rather > think that the following is correct, but for some reason it doesn't > work: > > arr.each do |a| > class_eval do > define_method( a ) ... > > For what the code is supposed to do, this seems like the right > function... but it does not work. Why? Why was a metaclass necessary? It does work, but it's not what you want. You don't want to define a method for the instances of Creature but rather for Creature itself. The Creature class is to set the standard values for "charisma" etc. > 2.. I still don't grasp exactly what class << self means. I know that > class << ClassName would add on to the class of ClassName. Please > explain This "class << ..." is simply the syntax for defining or opening the singleton class of an object: class << my_object # method definitions etc. end As you can see, it corresponds to the syntax of defining or opening a "normal" class: class MyClass # method definitions etc. end Well, and if you want to open/define the singleton class for "self", then you write class << self ... end -- Posted via http://www.ruby-forum.com/.