In article <4145CFE2.7070404 / thepierianspring.org>,
Randy W. Sims <ml-ruby / thepierianspring.org> wrote:
>gabriele renzi wrote:
>> Randy W. Sims ha scritto:
>> 
>>> Sounds sort of like a traits[1] or what Perl 6 refers to as 
>>> roles[2][3]. But, then this is deeper Ruby than I'm familiar with.
>> 
>> 
>> I think traits mostly are what we call modules
>
>I'm still learning Ruby, but aren't modules pretty much static? I think 
>of traits as a set of behaviors that an object can take on at any point 
>in it's lifetime. These behaviors are specific to that instance. They 
>are transient behaviors that can be taken on and latter shed off. You 
>would test for the presence of a trait before using it (much like 
>testing is_a?). etc.
>

You can always add more methods to a module as well as remove methods from 
a module:

irb(main):001:0> module Foo
irb(main):002:1>   def a      
irb(main):003:2>     "a"
irb(main):004:2>   end
irb(main):005:1>   def b
irb(main):006:2>     "b"
irb(main):007:2>   end
irb(main):008:1> end
=> nil
irb(main):009:0> class Bar
irb(main):010:1>   include Foo
irb(main):011:1> end
=> Bar
irb(main):012:0> b = Bar.new
=> #<Bar:0x400c3418>
irb(main):013:0> b.a
=> "a"
irb(main):014:0> b.b
=> "b"
irb(main):015:0> module Foo
irb(main):016:1>   undef b
irb(main):017:1> end
irb(main):018:0> b.b
NoMethodError: undefined method `b' for #<Bar:0x400c3418>
	from (irb):18
irb(main):019:0>  
irb(main):019:0>  module Foo
irb(main):020:1>    def c
irb(main):021:2>      "c"
irb(main):022:2>    end
irb(main):023:1> end
=> nil
irb(main):024:0> b.c
=> "c"

However, with traits & roles (and I'm certainly no expert) wouldn't it be 
better to have different modules that get mixed-in and 'unmixed'.

So you might have:

  class Baz
  end

  baz = Baz.new 
  baz.extend Foo  #baz now has Foo 'traits'

....and later you might want to do:
  baz.unextend Foo #remove the Foo 'traits' from baz

....later on you could add another set of traits to baz:
  baz.extend OtherTraits

Of course there is no 'unextend', but there are probably ways to 
implement this.  For example, you could get a list of all of the methods 
of Foo and undef them in the baz object (couldn't you?)

Phil