0

Given a Ruby class with a static method like

class Foo
  def self.bar
    puts "Hello"
  end
end

I always accessed the method via

Foo.bar

However, I just noticed that you can also do

Foo::bar

Why is that? Is there a semantic difference? My initial idea was that it's a matter of treating Foo as a class object (in which case the first version seems plausible) vs. treating it as a "namespace" like a module, in which case the second version makes sense.

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • 1
    How many answers you want ? http://stackoverflow.com/search?q=%5Bruby%5D+%22.%22+%22%3A%3A%22 :-) – Arup Rakshit Jun 11 '14 at 12:02
  • Argh, why didn't I find them... the Google-fu is weak with me. However, I think http://stackoverflow.com/questions/43134/is-there-a-difference-between-and-when-calling-class-methods-in-ruby is actually a better match for a "duplicate". – Frerich Raabe Jun 11 '14 at 12:04
  • I don't have access to vote for close.. You can mark it now dup with the one you just linked.. :) I am withdrawing mine – Arup Rakshit Jun 11 '14 at 12:05

0 Answers0