# Self in method name

**URL:** <https://www.ruby-forum.com/t/self-in-method-name/106582>\
**Category:** Ruby\
**Created:** [June 12, 2007, 6:19am UTC](https://www.ruby-forum.com/t/self-in-method-name/106582 "2007-06-12T06:19:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![iskaldur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@iskaldur](https://www.ruby-forum.com/u/iskaldur)\
**Post date:** [June 12, 2007, 6:19am UTC](https://www.ruby-forum.com/t/self-in-method-name/106582/1 "2007-06-12T06:19:01Z")

</div>

Is there any difference between using self in a method name, and not  
using it?  
For example, I’ve seen a lot something like

class Foo  
def self.bar  
…  
end  
end

Is this the same thing as

class Foo  
def bar  
…  
end  
end  
?

---

<div class="post-metadata">

**Author:** ![iskaldur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@iskaldur](https://www.ruby-forum.com/u/iskaldur)\
**Post date:** [June 12, 2007, 6:51am UTC](https://www.ruby-forum.com/t/self-in-method-name/106582/2 "2007-06-12T06:51:34Z")

</div>

iskaldur wrote:

> Is this the same thing as
> 
> class Foo  
> def bar  
> …  
> end  
> end  
> ?

I believe the first is equivalent to:

class Foo  
def Foo.bar  
…  
end  
end

and so quite different to the second.

---

<div class="post-metadata">

**Author:** ![iskaldur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@iskaldur](https://www.ruby-forum.com/u/iskaldur)\
**Post date:** [June 12, 2007, 7:22am UTC](https://www.ruby-forum.com/t/self-in-method-name/106582/3 "2007-06-12T07:22:35Z")

</div>

The first one creates a class method as opposed to the second one that  
creates an instance method (e.g., Foo.bar rather than x = Foo.new;  
x.bar;). 🙂

–Jeremy

On 6/12/07, iskaldur [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is this the same thing as

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![iskaldur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@iskaldur](https://www.ruby-forum.com/u/iskaldur)\
**Post date:** [June 12, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/self-in-method-name/106582/4 "2007-06-12T13:09:40Z")

</div>

Hi,

Am Dienstag, 12. Jun 2007, 13:19:02 +0900 schrieb iskaldur:

> class Foo  
> def self.bar  
> …  
> end  
> end
> 
> Is this the same thing as

class Foo  
class \<\<self  
def bar  
…  
end  
end  
end

Bertram

---

<div class="post-metadata">

**Author:** ![iskaldur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@iskaldur](https://www.ruby-forum.com/u/iskaldur)\
**Post date:** [June 12, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/self-in-method-name/106582/5 "2007-06-12T12:47:34Z")

</div>

Hi –

On Tue, 12 Jun 2007, iskaldur wrote:

> Is this the same thing as
> 
> class Foo  
> def bar  
> …  
> end  
> end

def self.bar is just a special case of:

def some\_object.some\_method

i.e., defining a singleton method on an object (a method that exists  
only in the method look-up for that particular object, not for other  
objects of its class). Actually it’s not even a special case; it’s  
just using the current value of self as the object on which to define  
the method.

At the top level of a class definition block, self is the class object  
– in this case, Foo. A singleton method defined on a class object  
(such as Foo.bar) is usually referred to as a class method. In  
addition, class methods are special-cased a bit, in comparison with  
other singleton methods… but that’s Lesson Two 🙂

David

---

<div class="post-metadata">

**Author:** ![iskaldur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@iskaldur](https://www.ruby-forum.com/u/iskaldur)\
**Post date:** [June 12, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/self-in-method-name/106582/6 "2007-06-12T13:19:41Z")

</div>

On 6/12/07, Bertram S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def bar
> ...
> end
> end
> 
> ```
> 
> end

Yes, except in the second you’re actually opening up the virtual class  
to work on it directly whereas in the first example you’re defining  
the method using an absolute name.

It’s easy to see this as a pattern though without understanding the  
background, which, hopefully, a brief example will rectify:

class \<\< Fixnum  
def x; “y”; end  
end

puts Fixnum.x # =\> “y”  
puts 10.x # ERROR

Note that you can dig into virtual classes at any time, not just  
within the capacity of a class you’re currently defining 🙂

So… when you see class \<\< self within the definition of another  
class, then it’s the equivalent of class \<\< ClassName, and merely  
using the “self” to provide the current class rather than naming it  
explicitly.

Cheers,  
Peter C.

> **[Ruby Inside: The Ruby Blog](http://www.rubyinside.com/)**
>
> The biggest Ruby and Rails blog since 2006 - tutorials, news, and lots more

---

<div class="post-metadata">

**Author:** ![iskaldur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@iskaldur](https://www.ruby-forum.com/u/iskaldur)\
**Post date:** [June 12, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/self-in-method-name/106582/7 "2007-06-12T18:11:10Z")

</div>

> class Foo  
> def self.bar  
> end
> 
> def bar  
> end  
> end

Arguably, the equivalent in Java is this:

public class Foo {  
public static void bar1() {  
}

public void bar2() {  
}  
}

Notice that you can’t use the same method name statically and  
as instance method in Java, whereas you can use the same name  
for the class method and the instance method in Ruby.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)
