From: Navindra Umanee Date: 2005-01-30T09:11:44+09:00 Subject: Re: pass block to a method call? Jeff Davis wrote: > That brings up another question: how do you call a function that accepts > a block if all you have is a proc object? > > i.e. what's the difference between the following: > def foo(&p) p.call; end > foo { puts 'foo' } > and: > def foo(&p) p.call; end > p = proc { puts 'foo' } > foo { p.call } > > Are those the same? Is the latter the proper way to pass a proc object > as a block? Yes they're the same. You know about "irb" right? You can try these out interactively like you do in Python. An easier way to handle the second call to foo is simply: foo(&p) The & specifies it's a block and is always the last argument. You can even do that when foo uses anonymous blocks. > Thanks to all for your help. I'm just getting into Ruby and I really > like it so far (coming from Python/Perl/PHP). I really recommend the pickaxe book if you haven't read it: http://www.rubycentral.com/book/ A more recent and up-to-date version is for sale. Cheers, Navin.