[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

reuse passed proc later?

David Gurba

10/30/2004 10:08:00 AM

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....

any nice solution?

David G.
--
_______________________________________________
Find what you are looking for with the Lycos Yellow Pages
http://r.lycos.com/r/yp_emailfooter/http://yellowpages.lycos.com/default.asp?S...




3 Answers

dblack

10/30/2004 10:23:00 AM

0

Robert Klemme

10/30/2004 12:37:00 PM

0


"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

Shashank Date

10/30/2004 12:43:00 PM

0

Hi,

David Gurba wrote:
> 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....
>
> any nice solution?

Did you mean to do this:

class A

attr_accessor :func

def foo(&block)
@func = block
end

end

a = A.new
a.foo { print "12" }
a.func.call

__END__

HTH,
-- shanko