Prev | Current Page 30 | Next

Brad Ediger

"Advanced Rails"

class_method_one; "Class method"; end
def self.class_method_two; "Also a class method"; end
class <def class_method_three; "Still a class method"; end
end
class <def class_method_four; "Yet another class method"; end
end
end
def A.class_method_five
"This works outside of the class definition"
end
class <def A.class_method_six
"You can open the metaclass outside of the class definition"
end
end
Figure 1-10. Singleton class of a class
Class:Object
(virtual)
Class:A
(virtual)
super
Object
A
super
klass
klass
Ruby Foundations | 17
# Print the result of calling each method in turn
%w(one two three four five six).each do |number|
puts A.send(:"class_method_#{number}")
end
# >> Class method
# >> Also a class method
# >> Still a class method
# >> Yet another class method
# >> This works outside of the class definition
# >> You can open the metaclass outside of the class definition
This also means that inside a singleton class definition??”as in any other class definition
??”self refers to the class object being defined. When we remember that the
value of a block or class definition is the value of the last statement executed, we can
see that the value of class <<returned from the class definition.


Pages:
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42