3

I'm curious about calling class methods and whether there is any difference between:

class Jt
  class << self
    def say_hello
      puts "I want to say hello"
    end
  end
end

class Jt2
  def self.say_hello
    puts "2 want to say hello"
  end
end

Jt.say_hello
Jt2.say_hello

Is it just style or is there any difference in how ruby handle these? I always use the latter for Rails stuff but tend to see the former in meta-programming or Rails source code.

sawa
  • 165,429
  • 45
  • 277
  • 381
timpone
  • 19,235
  • 36
  • 121
  • 211
  • Hmm... so that question + answer seems to be `how?` from a user point of view which I get (I think). I guess I'm just wondering if they are truly equivalent in terms of performance, resources etc... And whether one should be preferred over the other for non-stylistic reasons. – timpone Apr 24 '13 at 19:23
  • 1
    @sawa This question NOT a duplicate of that other one you linked to. That question is asking why "eigenclass" is different from "class"; that question is not talking about two different ways to define a method. – David Grayson Apr 24 '13 at 19:26
  • @DavidGrayson You are right. The question that I linked to is related but is not the same. – sawa Apr 24 '13 at 19:49

2 Answers2

1

I think the difference between those is just style. They both add a method to the singleton class of the class. Here is what I did with your code to investigate it:

class Jt
  class << self
    def say_hello
      puts "I want to say hello"
    end
  end
end

class Jt2
  def self.say_hello
    puts "2 want to say hello"
  end
end

p Jt.singleton_class.instance_method(:say_hello)   # => #<UnboundMethod: #<Class:Jt>#say_hello>
p Jt2.singleton_class.instance_method(:say_hello)  # => #<UnboundMethod: #<Class:Jt2>#say_hello>

In case it matters, I used JRuby.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • thx, my hunch is you're right and just stylistic. Just getting to know Ruby and would love to see if there was a `deeper` reason as the class method syntax seems ubiquitous in userspace apps while the eigenclass method seems pretty ubiquitous in frameworks, libraries, etc... – timpone Apr 24 '13 at 19:34
  • It is not just style. calling `<< self` involves more steps and uses more resource. When you define a method within `<< self`, the same method is defined in two different places. You can notice the difference by using `set_trace_func`. – sawa Apr 24 '13 at 19:42
  • thx @sawa , that seems logical but isn't it then surprising that it is the chosen way for writing libraries and frameworks. Not trying to be argumentative just trying to understand. – timpone Apr 24 '13 at 19:44
  • @timpone If you care about the resource, the latter way is better. But then, you have to repeat `self.` every time. Using `<< self` is (slightly more) resource consuming, but the programmer does not have to repeat `self.`. That is why some people chose that way. – sawa Apr 24 '13 at 19:47
  • thx both to David and @sawa for input. Feel more confident that these are functionally equivalent. – timpone Apr 24 '13 at 20:09
  • sawa, do you have any evidence? Could you show an example of how to use `set_trace_func` so I can understand what you are saying? What are the "two different places" where the method is defined? – David Grayson Apr 24 '13 at 21:02
0
class << self
    def say_hello
      puts "I want to say hello"
    end
end
  • is a singleton class inside class Jt.

Here is more informationWhat's the difference between a class and the singleton of that class in Ruby? and Look here Singleton class in Ruby

Community
  • 1
  • 1
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317