0

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.
Community
  • 1
  • 1
Kashyap
  • 15,354
  • 13
  • 64
  • 103
  • @WandMaker that one doesn't talk abt `Foo.method` way of defining. – Kashyap Feb 02 '16 at 15:46
  • `Foo.method` is similar to `self.method` except that it is typically used outside the `class Foo .... end` block. Note that `self == Foo` inside the class – Wand Maker Feb 02 '16 at 15:49
  • 2
    Neither of these define static methods. There are no static methods in Ruby. All methods are dynamic. There is only one kind of method in Ruby: instance methods. – Jörg W Mittag Feb 02 '16 at 15:50
  • @JörgWMittag that's just jargon of Rubyists who want to make ruby sound cool. To quote a few threads to dispute "there are no static methods in ruby": http://stackoverflow.com/questions/5231534/ruby-on-rails-static-method or http://stackoverflow.com/questions/2460067/what-does-a-java-static-method-look-like-in-ruby – Kashyap Feb 02 '16 at 15:57
  • 1
    The defining feature of a static method is that it is, well, static. In Ruby, all methods are dynamic. There is only one kind of method: dynamically dispatched instance methods. Rubyists *may* sometimes use the term "singleton method" to refer to an instance method of a singleton class, and they *may* use the term "class method" or "module function" to refer to a singleton method of a class or module (which of course is just a regular instance method of the singleton class of the class or module), and they *may* use the term "global method" to refer to a `private` instance method of `Object`. – Jörg W Mittag Feb 02 '16 at 16:03
  • 1
    But all of those are just the same: instance methods. And all of them are dispatched dynamically, not statically. There is no static dispatch in Ruby. – Jörg W Mittag Feb 02 '16 at 16:04
  • @JörgWMittag again, only a Rubyist would say the defining feature of 'static method' is it's dispatched statically. Ask 10 programmers why they use 'static methods' I bet more use it because of what wikipedia says ("Static methods are meant to be relevant to all the instances of a class rather than to any specific instance.") then how it's "dispatched". Anyway you've made me do the hard work and I found the answer, I'll post it soon. – Kashyap Feb 02 '16 at 16:45

0 Answers0