From: Robert Klemme Date: 2005-07-06T01:15:45+09:00 Subject: Re: Add methods to a predefined class: on the class instance or metaclass? Jacob Fugal wrote: > On 7/4/05, Robert Klemme wrote: >> >> class Foo >> def Foo.bar1() "bar1" end >> >> def self.bar2() "bar2" end >> >> class <> def bar3() "bar3" end >> end >> >> class <> def bar4() "bar4" end >> end >> end > > Note that the last form (class << Foo) doesn't need to be inside the > reopened class: > > $ cat > a.rb > class Foo; end > > class << Foo > def test > puts "Hello, world!" > end > end > > Foo.test > > $ ruby a.rb > Hello, world! The same is true for variant 1 also. I prefer the nested versions with "self" because they are immune to name changes of the class. Kind regards robert