Open classes, constructors and blocks

Hi all,

I'd like to re-open a class and to re-initialize its original constructor:

class Foo
def initialize
   yield self if block_given?
end
end

class Foo
attr_reader :var
alias foo_initialize initialize
def initialize
   foo_initialize
   @var = "Foo's instance var"
end
end

Foo.new { |foo| puts foo.var }

The problem is that the block passed to the new constructor is never called! Why?
Which is the best way to redefine a constructor when a class is re-opened?

Thanks!
Andrea

Alle 09:49, mercoledì 29 novembre 2006, Andrea Fazzi ha scritto:

alias foo_initialize initialize
def initialize
foo_initialize
@var = "Foo's instance var"
end
end

Foo.new { |foo| puts foo.var }

The problem is that the block passed to the new constructor is never
called! Why?

When you call foo_initialize from initialize, you're calling a completely
unrelated method, so the block won't be automatically passed to it (this is
different from when you use super; in that class the block is passed
automatically). In your case, you must pass the block in two ways: the first
is to create a new block which simply calls yield:

  def initialize
   @var="Foo's instance var"
   foo_initialize{yield self}
  end

(by the way, you need to assign @var before calling foo_initialize if you want
its value to be displayed, otherwise you'll get nil).
The other way is to pass the block as a variable to initialize, using the &
syntax:

  def initialize &block
   @var="Foo's instance var"
   foo_initialize &block
  end

I hope this helps

Stefano