From: Markus Gaelli Date: 2006-09-01T08:16:43+09:00 Subject: Fwd: [Q] How would you do method extensions (sometimes called class extensions) in Ruby? Begin forwarded message: > From: Sam Roberts > Date: August 31, 2006 11:44:20 PM GMT+02:00 > To: Markus Gaelli > Subject: Re: [Q] How would you do method extensions (sometimes > called class extensions) in Ruby? > > On Fri, Sep 01, 2006 at 06:08:11AM +0900, Markus Gaelli wrote: >> I am aware of Singleton Methods, but as they can extend instances, >> (how) can I extend classes without changing their original class >> definition in Ruby? > > This is probably best posted to ruby-talk, not ruby-core, folks would > likely be quite interested. > > classes are "open" in ruby, you can add methods as you wish. > > irb(main):001:0> s = "hi" > => "hi" > irb(main):002:0> class String; def this(opt) puts(self+opt) end end > => nil > irb(main):003:0> s.this(" that") > hi that > => nil > irb(main):004:0> "bye".this(" that") > bye that > => nil > > Cheers, > Sam >