[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Is better to subclass or to add methods to an existing class?
From:       dblack () candle ! superlink ! net
Date:       2002-09-19 13:49:29
[Download RAW message or body]

Hi --

On Thu, 19 Sep 2002, Jim Freeze wrote:

> On Thu, Sep 19, 2002 at 09:20:13PM +0900, Vincent Foley wrote:
> >
> > Can you enligthen me and tell me if it's really that bad an idea to add
> > methods to an existing class, or if he's just being a Python zealot?
> >
>
> I think it could be considered bad because it can produce
> alergic reactions between libraries.

Clearly this is a serious problem, though at the same time I think the
power of all this is just too great to go unused.

<signature_tune>

This is where the (stagnated, I'm afraid) Ruby Behaviors package comes
in.  I have to regard it as proof-of-concept more than anything
else... but anyway:

    class String
      def rot13
	"first version"
      end
    end

    require 'behaviors-0.0.1/lib/behaviors.rb'

    class Behavior
      class ROT13 < Definer
	def start
	  rot13 = "def rot13; 'behaviors version'; end"
	  define(String, "rot13", rot13)
	end
      end
    end

    b = Behavior.new(:ROT13)

    puts "abc".rot13
    b.adopt { puts "abc".rot13 }    # block form of adopt
    puts "abc".rot13

    # =>
    #   first version
    #   behaviors version
    #   first version

</signature_tune>


David

-- 
David Alan Black                      | Register for RubyConf 2002!
home: dblack@candle.superlink.net     | November 1-3
work: blackdav@shu.edu                | Seattle, WA, USA
Web:  http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic