bermonruf
12/12/2007 2:41:00 AM
Note: parts of this message were removed by the gateway to make it a legal Usenet post.
Take a look:
class Object
# Just to access the metaclass
def metaclass
class << self; self; end
end
end
class Foo
class << self # Inside the metaclass
def a # A: class method of Foo or method of the Foo's metaclass
"Class method";
end
def self.b # B: class method of the metaclass, or method of the
metaclass of Foo's metaclass
"Class method of the metaclass";
end
end
def self.c # C: same as A
"Class method";
end
def d # D: instance method
"Instance method";
end
def self.e # E: yet another example of A and C
class << self # Inside the metaclass of Foo, here we can call methods
like B
b;
end
end
end
["Foo.c", "Foo.metaclass.b", "Foo.c", "Foo.new.d", "Foo.e"].each do |code|
puts "#{code} \#=> #{eval(code)}";
end