Python is designed to support more than just object-oriented programming. Keeping the same interface between methods and functions allows the two styles to interact more cleanly.
Ruby was built from the ground up to be object oriented. Even literals are objects (rate 1.class and you will get Fixnum). The language was built in such a way that self is a reserved keyword that returns the current instance, wherever you are.
If you are inside an instance method of one of your classes, self is a reference to the specified instance.
If you are in the definition of the class itself (not in the method), self is the class itself:
class C puts "I am a #{self}" def instance_method puts 'instance_method' end def self.class_method puts 'class_method' end end
A class āI Cā will be printed in the class definition field.
The direct 'def' defines the instance method, while the 'def self.xxx' defines the class method.
c=C.new c.instance_method #=> instance_method C.class_method #=> class_method
webmat
source share