How to use Ruby's self keyword


Ruby Problem Overview

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?

Ruby Solutions

Solution 1 - Ruby

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
    "class method bar"

  def bar
    "instance method bar"
end  #returns "class method bar"

foo = #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 baz

  def self.success
    foo #looks for variable foo, doesn't find one, looks for class method foo, finds it, returns "foo!"

    baz #looks for variable baz, doesn't find one, looks for class method baz, doesn't find one, raises exception

  def instance_success
    baz #looks for variable baz, doesn't find one, looks for instance method baz, finds it, returns "baz!"

  def instance_fail
    foo #looks for variable foo, doesn't find one, looks for instance method foo, doesn't find one, raises exception

  def local_variable
    baz = "is my favorite method"
    baz #looks for variable baz, finds it, returns "is my favorite method"
  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"

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)

And in another method you call:

def other_method
  bar = "abcd"

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 = "abcd"

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

def example

def other_thing(example)

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.

Solution 2 - Ruby

In most cases is indeed redundant because you can just write foo for the same effect, but in this case it is not and the self is required.

var_one = method(args) will create a local variable called var_one, it will not call any method or do anything else to self.

self.var_one = method(args) will call the method var_one= on self with the argument method(args).

Another case where the use of self is non-optional would be if you want to pass it as an argument to a method, i.e. some_method(self) - you can't do that without the self keyword.

Solution 3 - Ruby

Here's an example:

def run miles
  self.miles = miles

In this case self will help. In most cases self is redundant.

Solution 4 - Ruby

One other use of self is to declare class methods (similar to static methods in Java).

class foo
  #do class related stuff here

That being said, you could also have used def instead for the method signature.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnkit SoniView Question on Stackoverflow
Solution 1 - RubyAndrewView Answer on Stackoverflow
Solution 2 - Rubysepp2kView Answer on Stackoverflow
Solution 3 - RubySohanView Answer on Stackoverflow
Solution 4 - RubybrayneView Answer on Stackoverflow