From: "Jan E." Date: 2012-04-11T13:49:07+09:00 Subject: Re: A Beginner's Question about Metaprogramming Phil Stone wrote in post #1055948: > Am I correct that the code below can't be rewritten, since it doesn't > deal with Singletons? (taking a risky assumption here) > > class_eval do > define_method( :initialize ) do > self.class.traits.each do |k,v| > instance_variable_set("@#{k}", v) > end > end > end You could leave out the "class_eval" and "define_method" and simply write "def initialize ... end" like you normally do, since this isn't a dynamic method definition at all: def initialize self.class.traits.each do |k,v| instance_variable_set("@#{k}", v) end end > Also, I've been really confused about the difference between classes and > objects. > I know that Objects are classes themselves, through "Object.class", > which returns "Class". This is a misunderstanding. Every object *has* a class, which defines the methods of the object. You can interpret classes as some kind of "construction plans" for objects. In most object oriented languages like Java or C++., classes aren't objects themselves but rather some kind of special entities. This isn't true for Ruby. In Ruby, classes are normal objects that behave like any other object. They can have instance variables and methods (like for example "superclass" or "attr_accessor"). The class of every class is "Class". > If "hello" is an instance and String is a class, what is an example of > an Object? Both "hello" and String are objects. "hello" is an instance of the class String, and String is an instance of the class Class. -- Posted via http://www.ruby-forum.com/.