Robert Klemme
10/30/2004 12:37:00 PM
"David Gurba" <blue_technx@lycos.com> schrieb im Newsbeitrag
news:20041030005556.A499DCA06F@ws7-4.us4.outblaze.com...
> Hello Everyone,
>
> I'm new to Ruby and some of its features...
>
> I want to do the following (with as little modfication as necessary):
>
> call a passed block to a function at some later time... Eg.
>
> class A
>
> def foo(&block)
> @func = block
> end
> end
>
> a = A.new
> num = a.func.call { print "12" }
>
> ..but func is an unknown method, and attr_accessor :func doesn't help
> either....
As David said, you don't invoke foo so you don't assign to @func. You got
the order a bit wrong since you provide the block too late, i.e. when you
want to call it. The smallest change to your example might be this
class A
def foo(&block)
@func = block
end
end
a = A.new
num = a.foo { print "12" }.call
But you probably wanted
class A
attr_reader :func
def foo(&b) @func = b end
end
a = A.new
a.foo { puts "blah!" }
a.func.call
Kind regards
robet