# What's the difference in calling method vs self.method?

**URL:** <https://www.ruby-forum.com/t/whats-the-difference-in-calling-method-vs-self-method/51464>\
**Category:** Ruby\
**Created:** [December 31, 2005, 7:50pm UTC](https://www.ruby-forum.com/t/whats-the-difference-in-calling-method-vs-self-method/51464 "2005-12-31T19:50:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [December 31, 2005, 7:50pm UTC](https://www.ruby-forum.com/t/whats-the-difference-in-calling-method-vs-self-method/51464/1 "2005-12-31T19:50:35Z")

</div>

Here’s some example code. In method\_b, what is the difference in  
calling method\_a and self.method\_a?

class HoHo

def method\_a  
puts “Hello, HoHo”  
end

def method\_b  
method\_a  
self.method\_a  
end

end

hoho = HoHo.new  
hoho.method\_b

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [December 31, 2005, 7:57pm UTC](https://www.ruby-forum.com/t/whats-the-difference-in-calling-method-vs-self-method/51464/2 "2005-12-31T19:57:55Z")

</div>

Jim wrote:

> Here’s some example code. In method\_b, what is the difference in  
> calling method\_a and self.method\_a?

I am sure that the more experienced coders will speak up soon, but until  
then Ill do my best.

The self. is implied when you call the method…  
just as self.puts or self.gets are often written puts or gets  
respectively…

Hope that makes sense.

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [December 31, 2005, 7:59pm UTC](https://www.ruby-forum.com/t/whats-the-difference-in-calling-method-vs-self-method/51464/3 "2005-12-31T19:59:48Z")

</div>

I see no difference indeed, but I’m a Ruby beginner…This is a Ruby  
shortcut I think, all method calls inside a class are supposed to be  
applied to the called “self” object. Maybe to avoid some “heavy” Python  
syntax in wich you’ll always see “this.method”.  
Am I wrong ?

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [December 31, 2005, 8:08pm UTC](https://www.ruby-forum.com/t/whats-the-difference-in-calling-method-vs-self-method/51464/4 "2005-12-31T20:08:49Z")

</div>

Jim wrote:

> Here’s some example code. In method\_b, what is the difference in  
> calling method\_a and self.method\_a?

I just had a thought that it may very well make a difference in the  
case of a conflict method names. For example if you had (puposely or  
not) created 2 method\_a’s in the same scope. Make sense?

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [December 31, 2005, 8:59pm UTC](https://www.ruby-forum.com/t/whats-the-difference-in-calling-method-vs-self-method/51464/5 "2005-12-31T20:59:57Z")

</div>

On Sat, 31 Dec 2005 19:11:37 -0000, Marcin MielÅ¼yÅ?ski  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> class D  
> end

Somewhat related to the above is the following case:

```
class Clz
  attr_accessor :myattr

  def amethod
    # doesn't work, Ruby treats as local assignment
    # myattr = 5

    # We have to be explicit.
    self.myattr = 5
  end
end

```

Another area I guess it could make a difference is:

```
class ClzToo

  def amethod
    # doesn't work, private methods cannot be called
    # with a receiver.
    # puts self.aprivate

    # So we can only call without 'self'
    puts aprivate
  end

  private

  def aprivate
    "private"
  end
end

```

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [December 31, 2005, 8:14pm UTC](https://www.ruby-forum.com/t/whats-the-difference-in-calling-method-vs-self-method/51464/6 "2005-12-31T20:14:50Z")

</div>

Jim wrote:

> Here’s some example code. In method\_b, what is the difference in  
> calling method\_a and self.method\_a?

In Your code it makes no difference, however there are situations when  
You’ll have to explicitly resolve method invocation, consider:

class C  
def meth1  
end

```
def meth2
	meth1	# no problem, but..
	meth1=4	# variable assignment
	self.meth1 # so we have to resolve it as a method
end

```

end

other thing is operator method invocation:

class D

```
def + arg
	p arg
end

def meth
	+ 2 # Ruby sees it as unary plus, aka +@
	self + 2 # explicit
end

```

end

lopex
