> It seems there are 3 ways of defining class methods (at least in common > usage): There's lots of ways to add a class method; Ruby is not a very prescriptive language. Two more interesting examples that are used: there's `instance_eval` on a Class instance: X = Class.new X.instance_eval do def foo; "calling #foo!"; end end X.foo # => "calling #foo!" or #class_eval on the class of your `Class` instance (which will be `Class`): X.class.class_eval do def bar; "calling #bar!"; end end X.bar # => "calling #bar!" It's not very complicated, but `class_eval` on a Class instance is typically used only in some rather advanced (and/or cryptic) metaprogramming, so you don't see it floating around a lot. ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Wed, Feb 16, 2011 at 21:55, botp <botpena / gmail.com> wrote: > On Thu, Feb 17, 2011 at 3:47 AM, Tony Arcieri <tony.arcieri / medioh.com> wrote: >> It seems there are 3 ways of defining class methods (at least in common >> usage): >> >> 1) Defining a method on self: >> 2) Opening up self and defining the methods directly: >> 3) Placing class methods into a module, and extending a class with that: > > 4) Explicit definition > > def C.m > ... > end > > i usually use this one since > 1 it's visibly specified. The definition matches the use. > 2 don't need to press pgup just to see who is self :) > 3 if i change class name, it self checks itself since i need to retype. > 4 less prone to self abuse :) > > best regards -botp > > > best regards -botp > >