Techniques for module to contribute to initialize() of its including classes

What are good design techniques for this e.g. if the module adds some
instance variables to its included class?

module M
  def f
    @f
  end
  # how to initialize @f ?
end

class A
  include M
end
Thanks.

itsme213 wrote:

What are good design techniques for this e.g. if the module adds some
instance variables to its included class?

module M
  def f
    @f
  end
  # how to initialize @f ?
end

class A
  include M
end
Thanks.

Do it like this and the module initialite will seamlessly integrate into
the class inheritance chain:

module M
  def initialize(*a,&b)
    super
    @f = 10
  end
end

There have been discussions about this
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/121611
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/129974

Kind regards

    robert

Robert Klemme wrote:

itsme213 wrote:

What are good design techniques for this e.g. if the module adds some
instance variables to its included class?

module M
def f
   @f
end
# how to initialize @f ?
end

class A
include M
end
Thanks.

Do it like this and the module initialite will seamlessly integrate into
the class inheritance chain:

module M
  def initialize(*a,&b)
    super
    @f = 10
  end
end

This does not work directly if the class itself defines an #initialize
as it will take precedence over the module version.

There have been discussions about this
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/121611
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/129974

Kind regards

    robert

E

It works if you call super from within the class initialize. The
module is inserted into the inheritance chain before any superclasses.

module M
  def f
    @f
  end
  # how to initialize @f ?
  def initialize(*args, &block)
    puts "in M.initialize"
    super
    @f = 10
  end
end

class A
  def initialize(*args, &block)
    puts "in A.initialize"
    super
  end
end

class B < A
  include M
  def initialize(*args, &block)
    puts "in B.initialize"
    super
  end
end

b = B.new
p b.f

__END__
in B.initialize
in M.initialize
in A.initialize
10

Sean

···

On 10/6/05, ES <ruby-ml@magical-cat.org> wrote:

Robert Klemme wrote:
> itsme213 wrote:
>
>>What are good design techniques for this e.g. if the module adds some
>>instance variables to its included class?
>>
>>module M
>> def f
>> @f
>> end
>> # how to initialize @f ?
>>end
>>
>>class A
>> include M
>>end
>>Thanks.
>
>
> Do it like this and the module initialite will seamlessly integrate into
> the class inheritance chain:
>
> module M
> def initialize(*a,&b)
> super
> @f = 10
> end
> end

This does not work directly if the class itself defines an #initialize
as it will take precedence over the module version.

easy:

   harp:~ > cat a.rb
   require 'traits'

   module M
     trait 'f' => 42
   end

   class C
     include M
   end

   c = C::new

   p c.f

   harp:~ > ruby a.rb
   42

hard:

   harp:~ > cat a.rb
   module M
     ATTRIBUTES = {
       'f' => 42
     }

     def self::inited_attr a
       module_eval <<-code
         def #{ a }
           __m_init
           @#{ a }
         end
       code
     end

     ATTRIBUTES.keys.each{ |a| inited_attr a }

     def __m_init
       return if @__m_init
       ATTRIBUTES.each{ |a,v| instance_variable_set "@#{ a }", v }
       @__m_init = true
     end
   end

   class C
     include M
   end

   c = C::new

   p c.f

   harp:~ > ruby a.rb
   42

hth.

btw. i just broke the traits installer so download v 0.6.0 (not 0.7.0) if you
get it - i'm working on a fix now...

-a

···

On Fri, 7 Oct 2005, ES wrote:

Robert Klemme wrote:

itsme213 wrote:

What are good design techniques for this e.g. if the module adds some
instance variables to its included class?

module M
def f
   @f
end
# how to initialize @f ?
end

class A
include M
end
Thanks.

Do it like this and the module initialite will seamlessly integrate into
the class inheritance chain:

module M
  def initialize(*a,&b)
    super
    @f = 10
  end
end

This does not work directly if the class itself defines an #initialize
as it will take precedence over the module version.

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna

===============================================================================

Sean O'Halpin wrote:

Robert Klemme wrote:

itsme213 wrote:

What are good design techniques for this e.g. if the module adds some
instance variables to its included class?

module M
def f
  @f
end
# how to initialize @f ?
end

class A
include M
end
Thanks.

Do it like this and the module initialite will seamlessly integrate into
the class inheritance chain:

module M
def initialize(*a,&b)
   super
   @f = 10
end
end

This does not work directly if the class itself defines an #initialize
as it will take precedence over the module version.

It works if you call super from within the class initialize. The
module is inserted into the inheritance chain before any superclasses.

Huh. Should have known better than to try it in irb :slight_smile:
It does seem to work fine. Ignore me.

module M
  def f
    @f
  end
  # how to initialize @f ?
  def initialize(*args, &block)
    puts "in M.initialize"
    super
    @f = 10
  end
end

class A
  def initialize(*args, &block)
    puts "in A.initialize"
    super
  end
end

class B < A
  include M
  def initialize(*args, &block)
    puts "in B.initialize"
    super
  end
end

b = B.new
p b.f

__END__
in B.initialize
in M.initialize
in A.initialize
10

Sean

E

···

On 10/6/05, ES <ruby-ml@magical-cat.org> wrote: