Can anyone explain to me what the meaning of adding self
to the method definition is? Is it similar to the this
keyword in java?

- 139,698
- 36
- 220
- 238

- 9,100
- 25
- 72
- 109
3 Answers
Contrary to other languages, Ruby has no class methods, but it has singleton methods attached to a particular object.
cat = String.new("cat")
def cat.speak
'miaow'
end
cat.speak #=> "miaow"
cat.singleton_methods #=> ["speak"]
def cat.speak
creates a singleton method attached to the object cat.
When you write class A
, it is equivalent to A = Class.new
:
A = Class.new
def A.speak
"I'm class A"
end
A.speak #=> "I'm class A"
A.singleton_methods #=> ["speak"]
def A.speak
creates a singleton method attached to the object A. We call it a class method of class A.
When you write
class A
def self.c_method
'in A#c_method'
end
end
you create an instance of Class
(*). Inside the class definition, Ruby sets self to this new instance of Class, which has been assigned to the constant A. Thus def self.c_method
is equivalent to def cat.speak
, that is to say you define a singleton method attached to the object self, which is currently the class A.
Now the class A has two singleton methods, that we commonly call class methods.
A.singleton_methods
=> ["c_method", "speak"]
(*) technically, in this case where A
has already been created by A = Class.new
, class A
reopens the existing class. That's why we have two singleton methods at the end. But in the usual case where it is the first definition of a class, it means Class.new
.

- 19,179
- 10
- 84
- 156

- 3,674
- 2
- 15
- 10
-
13This is the correct answer. It is true that experienced Rubyists sometimes use the term "class method" to mean "instance method of the class object's singleton class", but they only use that shorthand in the full understanding that *there is no such thing as a class method* in Ruby. (In fact, there is no such thing as a "singleton method" either: singleton methods are just regular methods which happen to be defined in the singleton class of an object.) The OP, however, is obviously *not* an experienced Rubyist, therefore confusing him with shorthand is not a good idea. Thumbs up! – Jörg W Mittag Dec 05 '12 at 13:37
-
Does this meaning change when referring to a module rather than a class? – Adrian May 28 '21 at 05:09
-
@Adrian - functionally it works the same. For example, if you have `def self.bar` inside module `Foo`, then you could call `Foo.bar`. However, there is a difference in that modules are technically instances of the class `Module` which is an instance of the class `Class` (ie the class of all classes). This is different than, for example `A` in Bernards answer above, which is "a class", which is simply an instance of `Class`. Hope this helps! – Clark Taylor Aug 20 '21 at 16:18
In ruby self is somewhat similar to this
in java, but when it comes to classes its more like the keyword static
in java. A short example:
class A
# class method
def self.c_method
true
end
# instance method
def i_method
true
end
end
A.c_method #=> true
A.i_method #=> failure
A.new.i_method #=> true
A.new.c_method #=> failure
Update: Difference between static methods in java and class methods in ruby
Static methods in Java have two distinct features that makes them different from instance methods: a) they are static, b) they are not associated with an instance. (IOW: they really aren't like methods at all, they are just procedures.) In Ruby, all methods are dynamic, and all methods are associated with an instance. In fact, unlike Java where there are three different kinds of "methods" (instance methods, static methods and constructors), there is only one kind of method in Ruby: instance methods. So, no: static methods in Java are completely unlike methods in Ruby. – Jörg W Mittag 1 hour ago

- 3,699
- 24
- 34
-
It is *nothing at all* like `static`. In particular, those methods are dynamic, not static. – Jörg W Mittag Dec 04 '12 at 17:33
-
1if you define a `Class#method` in ruby with def `self.method ...` its kinda like `class JavaClass; public static void method ... ` in java – krichard Dec 04 '12 at 17:57
-
3Static methods in Java have two distinct features that makes them different from instance methods: a) they are static, b) they are not associated with an instance. (IOW: they really aren't like methods *at all*, they are just procedures.) In Ruby, *all* methods are dynamic, and *all* methods are associated with an instance. In fact, unlike Java where there are three different kinds of "methods" (instance methods, static methods and constructors), there is only *one* kind of method in Ruby: instance methods. So, no: static methods in Java are completely unlike methods in Ruby. – Jörg W Mittag Dec 05 '12 at 13:33
When declaring a method, the self
of the declaration is the declaring class/module, so effectively you are defining a class method. For the client, this works similar to a static
method in java. The client would call the method on the class instead of an instance: MyClass.method
Here you can find some more details on class and instance methods.
EDIT: While the self
keyword is akin to the this
keyword in java, the effects of using self
for class method declaration are similar to the effect of using the static
keyword in java. The similarity is that static methods in java, like class methods in ruby are accessed using the class object iself instead of an instance of the class.
Please note that static
does not stand for the opposite of dynamic. The choice of the name for this keyword is questionable (probably inherited from C) and rather should have been called perClass
or similar to better reflect the meaning. The technical meaning is that all static
members exist only once for each classloader.

- 60,521
- 48
- 179
- 224
-
2It is *nothing at all* like `static`. In particular, those methods are dynamic, not static. – Jörg W Mittag Dec 04 '12 at 17:32
-
6@JörgWMittag: it is like Java's `static` in at least one sense -- it applies to the class level rather than the instance level. – Eric Walker Dec 04 '12 at 17:48
-
7@JörgWMittag -
is a rather strong term for something with distinct similarities (and surely there are differences). have added some info to clarify my viewpoint. – kostja Dec 05 '12 at 10:06 -
Static methods in Java have two distinct features that makes them different from instance methods: a) they are static, b) they are not associated with an instance. (IOW: they really aren't like methods *at all*, they are just procedures.) In Ruby, *all* methods are dynamic, and *all* methods are associated with an instance. In fact, unlike Java where there are three different kinds of "methods" (instance methods, static methods and constructors), there is only *one* kind of method in Ruby: instance methods. So, yes: static methods in Java are completely unlike methods in Ruby. – Jörg W Mittag Dec 05 '12 at 13:31
-
1@kostja I think in this context, `static` in Java refers to static binding (i.e. which method to call is determined at compile time), as opposed to dynamic binding which is used for instance methods. So in this sense, it does stand for the opposite of dynamic, but is true the keyword static has an unpractical amount of overloaded meanings, esp. in C. – PieterNuyts May 20 '19 at 09:29
-
1yeah, my first error was to say 'is like static in Java'. I was concerned only with the method binding. Then the discussion got out of hand :) have changed the initial wording now. – kostja May 20 '19 at 09:37