39

This is not specific for Rails - I am just using Rails as an example.

I have a model in Rails:

class Item < ActiveRecord::Base

  def hello
    puts "Hello, #{self.name}"
  end
end

(Let's assume that the Item model (class) has a method called name.) When do I need to use self.name and when can I just use name (e.g., #{name})?

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
jriff
  • 1,947
  • 3
  • 23
  • 33

3 Answers3

67
  1. It is idiomatic to prefer to omit self. when invoking methods; it is generally never needed.

  2. You must use self.foo = xxx when calling a setter method, instead of foo = xxx, so that Ruby realizes that you are not trying create a new local variable.

    • Similarly, in the unlikely event that you have an existing local variable do_something with the same name as a method, you must use self.do_something to invoke the method, as just do_something will end up reading the variable.
  3. You cannot use self.foo(...) to call a private method; you must instead call just foo(...).

Phrogz
  • 296,393
  • 112
  • 651
  • 745
10

If you omit self Ruby will first look for local variables with that name, then for an instance method. It's not idiomatic to write self.. In any case, you have to write self.something = value on assignations.

Note that you cannot use self when calling private methods (no problem with protected methods):

class A
  def foo; self.bar; end

private

  def bar; "bar"; end
end

A.new.foo  
# private method `bar' called for #<A:0x7f49193584f0> (NoMethodError)
tokland
  • 66,169
  • 13
  • 144
  • 170
0

Following this tutorial, you have no need to use self pointer. But i think this (or self in our case) pointers are used to resolve name conflicts. Actually, @name and self.name are the same statements (if there is no name method for your class). E.g.:

class Moo
  attr_accessor :name

  def moo(name)
    name = name # O_o which *name* should i use?
  end

  def foo(name)
    @name = name # the same as *self.name = name*
  end

  def hello
    puts self.name # the same as *puts @name*
  end
end

a = Moo.new
a.hello() # should give no output

a.moo('zaooza')
a.hello() # Hey! Why does it prints nothing?

a.foo('zaooza')
a.hello() # whoa! This one shows 'zaooza'!

Try running this code and you'll see =)

shybovycha
  • 11,556
  • 6
  • 52
  • 82
  • 4
    Also a special case: methods named `foo=` MUST be called via `self.foo=`, otherwise it defines a new variable named `foo`. – Marcel Jackwerth Jan 15 '11 at 13:14
  • Yeah, you're right. Sorry, i've overlooked that and totally forgot about operators... – shybovycha Jan 15 '11 at 13:17
  • 5
    `@name` and `self.name` are definitely *not* the same statement. `self.name` is a method call and `@name` is an instance variable. It's just that in your example (but not in the OP's), `self.name` is a method which happens to return `@name`. In the OP's example `self.name` will get the value stored in the database and `@name` will simply be `nil`, so they're definitely not the same. – sepp2k Jan 15 '11 at 15:31
  • 3
    @shybovycha: Nope, that's not corrected. If there is no `name` method, `self.name` will cause a `NoMethodError`. `self.name` is always a method call. It is never a variable (though it might very well return the contents of a variable). – sepp2k Jan 15 '11 at 18:35
  • sepp is right. also idiomatically, I don't think I have ever seen anyone use empty parens on a method call before in ruby. – Matt Briggs Jan 15 '11 at 18:42