From: Howard Roberts Date: 2009-12-11T20:38:05+09:00 Subject: Re: solve this ruby problem Enrique Comba Riepenhausen wrote: > For more on Singletons you can refer to: > > - http://en.wikipedia.org/wiki/Singleton_pattern Please don't get the above information about the 'Singleton Pattern' confuse your efforts to understand Ruby's very special use of the term Singleton Class, which is completely different. Jim Weirich does a good job of explaining this confusion here: http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red Hopefully someone more well versed in the subject than me will chime in soon, but as this is a trap that got me for, I can tell you this: (pretty much) Everything in Ruby is a class, and every class (including your MyLogger class) has a hidden, anonymous class where methods are stored. This is called the Singleton class (or eigenclass, or metaclass-- don't be too worried about the name- it's been point of discussion for lots of people). One reason this Singleton class exists is to be able to place methods like your MyLogger.create into the object's (everything in Ruby is an object) lookup chain somewhere, and to give you the ability to change what the context and scope of variables (and what 'self' refers to) that you use. There are several ways to create a singleton class-- your def MyLogger.create method is perhaps the most basic. Once someone gets further along in learning Ruby they might need to use methods like class_eval, method_missing, instance_eval, creating methods dynamically, and need to start mixing in methods from modules into objects dynamically. Once this point is reach a good grasp of Ruby's concept of the Singleton Class is required. In the meantime, a beginner needs to know that every object does have a Singleton Class, and that 'Metaprogramming' is just plain old Ruby that manipulates the Singleton Class in very useful ways. HTH, Howard -- Posted via http://www.ruby-forum.com/.