From: Florian Gilcher Date: 2013-02-23T04:53:08+09:00 Subject: Re: Class method's has a fuzzy meaning and a sharp meaning Hi, Actually, I groked that concept after I understood that "instance methods" are actually the special ones. I'll ignore Modules for this explanation. They make things a tiny bit more complex, but without a lot of conceptual gain. In Ruby, callable methods are bound to objects. Classes are objects, so they have methods. Those methods are defined directly on the object itself. All of the following 3 are equivalent: class Foo def self.bar #self is "Foo" in this context! end def Foo.bar end a = self def a.bar end end Which means that they can be directly called: Foo.bar Takeaway: you can, at any time, define a method on any object in Ruby. Even on classes. Now, the real problem - and thats where the Class/Object distinction comes into play: how do I define methods on Objects that do not exist yet? Thats what instance methods do: you define a method without an explicit object. class Foo def bar end end If we inspect those two methods, they have an interesting difference: [3] pry(main)> Foo.method(:bar) => # [4] pry(main)> Foo.instance_method(:bar) => # The first one is _bound_ to an object (the class Foo) and thus can be called on it. The second on is _unbound_, which also means that it cannot be called, because it is not _bound_ yet, making things like "self" meaningless: [5] pry(main)> Foo.method(:bar).call => nil [6] pry(main)> Foo.instance_method(:bar).call NoMethodError: undefined method `call' for #... Now, the special ability of classes comes into play: they can be _instantiated_, which means that they can create objects that gain all those instance methods as normal methods: [7] pry(main)> Foo.new.method(:bar) => # Note the slightly different notation: thats Foo#bar, the instance methods, not Foo.bar, the class method. So, I usually remember the whole system using 2 simple rules: * You can define and call methods on objects * You can define methods that serve as templates for methods that _future_ objects will gain. Those are instance methods. I hope that helped. Regards, Florian P.S.: You can really define methods on any object at any time: [11] pry(main)> def bla [11] pry(main)* s = "string" [11] pry(main)* def s.really? [11] pry(main)* "yes" [11] pry(main)* end [11] pry(main)* s [11] pry(main)* end => nil [12] pry(main)> bla.really? => "yes" On Feb 21, 2013, at 9:09 PM, Xavier R. wrote: > David A. Black in his book - "The Well-Grounded Rubyist" > > "So class method has a fuzzy meaning and a sharp meaning. Fuzzily, any > method that gets called directly on a Class object is a class method. > Sharply, a class method is defined, not just called, directly on a Class > object. You’ll hear it used both ways, and as long as you’re aware of > the underlying engineering and can make the sharp distinctions when you > need to, you’ll be fine." > > As a beginner couldn't take the concept at all. So anyone out there - > could you help me the above with some examples? > > -- > Posted via http://www.ruby-forum.com/.