[Q] How would you do method extensions (sometimes called class extensions) in Ruby?

From: Markus Gaelli <gaelli@emergent.de>
Date: September 1, 2006 1:04:50 AM GMT+02:00
To: ruby-core@ruby-lang.org
Subject: Re: [Q] How would you do method extensions (sometimes called class extensions) in Ruby?

Sam and Nicolas,

thanks for your fast answers and pointers.
Guess I have to subscribe to another list - which is a good thing!
- Just wanted to be sure... :wink:
(I therefore forward another mail which just happened to arrive in this moment in my squeak mailbox)

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

I was reading (skimming) the Ruby book of Thomas and Hunt but could not find this. Thanks a lot!
I appreciate Ruby to be able to extend any class with any methods.

Cheers,

Markus

p.s. Funny that you have "reply" set to the person who answered and not to the list, different than in Squeak and Squeakland...

···

Begin forwarded message:

On Aug 31, 2006, at 11:44 PM, Sam Roberts wrote:

On Fri, Sep 01, 2006 at 06:08:11AM +0900, Markus Gaelli wrote: