"Jeff Davis" <jdavis-list / empires.org> schrieb im Newsbeitrag news:41FC2458.4060009 / empires.org... > >>The only difference is that in the second case the block is anonymous. >>In the first case, it has a name and so you can pass it around if you >>want (e.g. recursively). >> >> > 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? As others have pointed out already this one is better (and IMHO faster although I did not test that): irb(main):006:0> def f(&b) b.call end => nil irb(main):007:0> foo = lambda {puts "ja"} => #<Proc:0x101a84f8@(irb):7> irb(main):008:0> f &foo ja => nil I think nobody mentioned this before (sorry, if I overread that): An advantage of the idiom "&b" is that you can easily pass on a block: def g(&b) b.call end def f(&b) g &b end irb(main):009:0> def g(&b) b.call end => nil irb(main):010:0> def f(&b) g &b end => nil irb(main):011:0> f { puts "ja" } ja => nil > Thanks to all for your help. I'm just getting into Ruby and I really like > it so far (coming from Python/Perl/PHP). You're welcome! Kind regards robert