As for Python, I can't tell you anything new. self usually passed as the first parameter to the method, as pst said. From Python Docs
Often the first argument to a method is called self. This is nothing more than a convention: the name "I" is completely irrelevant to Python. Please note, however, that by not complying with the convention, your code may be less readable by other Python programmers, and it is also possible that a class browser program may be written that relies on such convention.
CRUBY (or "MRI") has something similar that happens under the hood. Each C extension can define methods (module / class / singleton) in a Ruby class using
- rb_define_method (instance)
- rb_define_singleton_method (singleton class)
- rb_define_module_function (class / module)
The actual implementation functions always take VALUE self as their first argument, similar to the Python idiom. self in these cases refers to the instance of the object to which this particular message is sent, i.e. if you have
person = Person.new person.do_sth
and do_sth will be implemented in C, then there will be a corresponding C function
VALUE person_do_sth(VALUE self) { //do something return self; }
Each such implementation should return a VALUE (C representation of the Ruby object), which refers to the fact that every method call or message sent (stick to the Smalltalk language) has a return value in Ruby. There is no void function in Ruby.
Although we need to pass self back and forth in low-level C code, you do not need to do this in Ruby code, Ruby will take care of this for you. The current value of self is stored internally in the current context of the thread that is running, so the existence of self provided, the message "self" will always evaluate some object.
Due to the dynamic nature of Ruby, the actual value of this object referenced by self changes with the current area of ββcode that is currently being interpreted. Run this to see for yourself:
puts "#{self} - declared in global scope"
If you want to call the method / send the message from any context to self , you can do it explicitly (for example, self.method ) or omit self as the recipient - then, by convention, the implicit recipient of the message will be self .
An interesting note to this is the interpretation of Ruby's private methods, which differs, for example, from the concept of Java private . Ruby's private methods can only be called by sending a message using self as an implicit receiver, i.e.
class A def a b end private def b puts "I'm private" end end a = A.new aa
works, while replacing method a with
def a self.b end
will raise an exception. That means something very common in Java
class A { private boolean compareUs(A a1, A a2) { ... } public boolean equals(A a1, A a2) { return (a1.compareUs() == a2.compareUs()); } }
will not work in Ruby. A stupid example, but just to illustrate: in Java we can access the private methods of other instances of the same class, this is not possible in Ruby, because we can only access private methods of the current self .
Finally, to complicate things a bit, the instance_eval and class_eval functions will also change the value of self at runtime.