Stefano Crocco
11/29/2006 9:07:00 AM
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