Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Ruby's self keyword

From what I understand about self, it refers to the current instance of the class.

Isn't this the default behaviour at all times anyways? For example, isn't

self.var_one = method(args) 

equivalent to

var_one = method(args)  

If so, what is the use of self?

like image 439
Ankit Soni Avatar asked Jul 12 '11 18:07

Ankit Soni


People also ask

How do you use the self method in Ruby?

self is a special variable that points to the object that "owns" the currently executing code. Ruby uses self everwhere: For instance variables: @myvar. For method and constant lookup.

What does self refer to in an instance method body?

the method self refers to the object it belongs to. Class definitions are objects too. If you use self inside class definition it refers to the object of class definition (to the class) if you call it inside class method it refers to the class again.

What is a Ruby method?

A method in Ruby is a set of expressions that returns a value. Within a method, you can organize your code into subroutines which can be easily invoked from other areas of their program. A method name must start a letter or a character with the eight-bit set.


1 Answers

There are several important uses, most of which are basically to disambiguate between instance methods, class methods, and variables.

First, this is the best way to define class methods:

class Foo   def self.bar     "class method bar"   end    def bar     "instance method bar"   end end  Foo.bar  #returns "class method bar"  foo = Foo.new foo.bar #returns "instance method bar" 

Also, within instance methods self refers to the instance, within class methods it refers to the class, and it can always be used to distinguish from local variables.

class Bar   def self.foo     "foo!"   end    def baz     "baz!"   end    def self.success     foo #looks for variable foo, doesn't find one, looks for class method foo, finds it, returns "foo!"   end    def self.fail     baz #looks for variable baz, doesn't find one, looks for class method baz, doesn't find one, raises exception   end    def instance_success     baz #looks for variable baz, doesn't find one, looks for instance method baz, finds it, returns "baz!"   end    def instance_fail     foo #looks for variable foo, doesn't find one, looks for instance method foo, doesn't find one, raises exception   end    def local_variable     baz = "is my favorite method"     baz #looks for variable baz, finds it, returns "is my favorite method"   end    def disambiguate     baz = " is my favorite method"     self.baz + baz #looks for instance method baz, finds it, looks for local variable baz, finds it, returns "baz! is my favorite method"   end end 

So, in the end, you can avoid using self in many cases, but it's often helpful to use it to make sure that you don't inadvertently create naming conflicts later on. Sometimes those can create bugs that are very hard to find. In the end it's often a matter of personal style.


As noted in the comments, one more really important thing:

In a class, if you have a method like this:

def bar=(string)   ... end 

And in another method you call:

def other_method   bar = "abcd" end 

It isn't going to call your bar= method, it's going to create a local variable bar. So, in this case you use self to tell Ruby not to create a local variable:

def other_method   self.bar = "abcd" end 

The same thing applies if you want to take an argument with the name of a method:

def example   ... end  def other_thing(example)   self.example(example) end 

If you left off self Ruby would assume you meant the local variable with the same name.

So, in general, self in method names is used to distinguish between class and instance variables, and everywhere else you use it when Ruby needs help distinguishing between method calls and local variables or local variable assignment.

I hope that makes sense.

like image 93
Andrew Avatar answered Sep 22 '22 21:09

Andrew