By jriff


2011-01-15 12:57:21 8 Comments

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 say 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? (Eg. #{name})

3 comments

@Phrogz 2011-01-15 18:30:58

  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(...).

@tokland 2011-01-15 13:05:32

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)

@shybovycha 2011-01-15 13:04:48

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 =)

@Marcel Jackwerth 2011-01-15 13:14:50

Also a special case: methods named foo= MUST be called via self.foo=, otherwise it defines a new variable named foo.

@shybovycha 2011-01-15 13:17:51

Yeah, you're right. Sorry, i've overlooked that and totally forgot about operators...

@sepp2k 2011-01-15 15:31:20

@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 2011-01-15 18:35:28

@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).

@Matt Briggs 2011-01-15 18:42:22

sepp is right. also idiomatically, I don't think I have ever seen anyone use empty parens on a method call before in ruby.

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] How to understand nil vs. empty vs. blank in Rails (and Ruby)

  • 2009-05-19 22:51:36
  • Arrel
  • 303945 View
  • 1117 Score
  • 14 Answer
  • Tags:   ruby-on-rails ruby

26 Answered Questions

[SOLVED] How to check if a value exists in an array in Ruby

  • 2009-12-31 17:49:03
  • user211662
  • 850480 View
  • 1294 Score
  • 26 Answer
  • Tags:   ruby arrays

20 Answered Questions

[SOLVED] How to call shell commands from Ruby

  • 2008-08-05 12:56:52
  • CodingWithoutComments
  • 520063 View
  • 1065 Score
  • 20 Answer
  • Tags:   ruby shell interop

10 Answered Questions

[SOLVED] How to convert a string to lower or upper case in Ruby

24 Answered Questions

[SOLVED] How to write a switch statement in Ruby

6 Answered Questions

[SOLVED] Why is it bad style to `rescue Exception => e` in Ruby?

6 Answered Questions

[SOLVED] class << self idiom in Ruby

19 Answered Questions

[SOLVED] What is attr_accessor in Ruby?

  • 2010-12-06 21:07:06
  • dennismonsewicz
  • 458390 View
  • 1011 Score
  • 19 Answer
  • Tags:   ruby

5 Answered Questions

[SOLVED] How do I get the name of a Ruby class?

36 Answered Questions

[SOLVED] What does Ruby have that Python doesn't, and vice versa?

  • 2009-07-11 12:24:26
  • Lennart Regebro
  • 126496 View
  • 263 Score
  • 36 Answer
  • Tags:   python ruby

Sponsored Content