Tutorial by Examples

In Ruby, there is always an implicit receiver for all method calls. The language keeps a reference to the current implicit receiver stored in the variable self. Certain language keywords like class and module will change what self points to. Understanding these behaviors is very helpful in mastering...
When you define a class or module, the implicit receiver becomes a reference to the class itself. For example: puts "I am #{self}" class Example puts "I am #{self}" end Executing the above code will print: "I am main" "I am Example"
Most Ruby code utilizes the implicit receiver, so programmers who are new to Ruby are often confused about when to use self. The practical answer is that self is used in two major ways: 1. To change the receiver. Ordinarily the behavior of def inside a class or module is to create instance methods...

Page 1 of 1