Are these just different ways of defining a static method in Ruby?

82 Views Asked by At

Based on what I know, yes they are, but I'm noob at ruby and want to be sure. Are there differences in what they do? Is there a reason to use one syntax over the other?

I do know that class << self and def self.method behave differently if used outside the context of class/module, because self is different.

class Foo
  def m1
    'm1'
  end

  class << self # 1
    def m2
      'm2'
    end
  end

  def self.m3 # 2
    'm3'
  end

  def Foo.m4 # 3
    'm4'
  end
end

Foo.singleton_methods
# => [:m2, :m3, :m4]

I've seen class << self vs self.method with Ruby: what's better? and it doesn't talk about Foo.method way of declaring.


as I can't post an answer, I'll add the answer here:

Looks like my question is tailor made for this section in ruby spec (or http://www.ipa.go.jp/osc/english/ruby/ in case the direct link doesn't work).

In short, these are 3 different ways of defining a singleton method in Ruby. And:

Singleton methods of a class is similar to so-called class methods in other object-oriendted languages because they can be invoked on that class.


39 6.5.3 Singleton classes
40 Every object, including classes, can be associated with at most one singleton class. The singleton
41 class defines methods which can be invoked on that object. Those methods are singleton methods
42 of the object. If the object is not a class, the singleton methods of the object can be invoked
43 only on that object. If the object is a class, singleton methods of the class can be invoked only
44 on that class and its subclasses (see 6.5.4).
14
1  A singleton class is created, and associated with an object by a singleton class definition (see
2  13.4.2) or a singleton method definition (see 13.4.3).
3  EXAMPLE 1 In the following program, the singleton class of x is created by a singleton class definition.
4  The method show is called a singleton method of x, and can be invoked only on x.
5  x = "abc"
6  y = "def"
7
8  # The definition of the singleton class of x
9  class << x
10   def show
11     puts self # prints the receiver
12   end
13 end
14
15 x.show # prints abc
16 y.show # raises an exception
17 EXAMPLE 2 In the following program, the same singleton method show as EXAMPLE 1 is defined
18 by a singleton method definition. The singleton class of x is created implicitly by the singleton method
19 definition.
20 x = "abc"
21
22 # The definition of a singleton method of x
23 def x.show
24   puts self # prints the receiver
25 end
26
27 x.show
28 EXAMPLE 3 In the following program, the singleton method a of the class X is defined by a singleton
29 method definition.
30 class X
31 # The definition of a singleton method of the class X
32 def X.a
33   puts "The method a is invoked."
34 end
35 end
36 X.a
37 NOTE Singleton methods of a class is similar to so-called class methods in other object-oriendted
38 languages because they can be invoked on that class.
0

There are 0 best solutions below