From: Gavin Sinclair Date: 2004-08-06T00:24:41+09:00 Subject: Re: Macros in Ruby On Thursday, August 5, 2004, 7:56:34 PM, Kristof wrote: > Hi, > On Thu, 05 Aug 2004 03:30:33 +0000, Jesse Jones wrote: >>> Which do you prefer? >>> >>> (1..10).map { |n| n ** 2 } >>> >>> (1..10).map( lambda { |n| n ** 2 } ) > shouldn't that be '(1..10).map( &lambda { |n| n ** 2 } )'? No. Jesse expressed a distaste for implicitly passed arguments (i.e. a block being passed to the method as a hidden, or a special, parameter. So I presented the above examples as two *different* possible #map interfaces. In the first one, the block "parameter" is implicit; in the second one, explicit. >> >> I prefer a light-weight syntax for something as commonly used as anonymous >> functions. On the other hand, I still don't know why {|n| n ** 2} can't >> simply create a proc object. > It can! Wether or not a proc object gets created depends on the > called method. [...] That's context-dependent. My impression is that Jesse thinks that code snippet in *any* context should evaluate to a lambda. > On the other hand, a Proc created this way is different from one > created using Proc.new or lambda. > The difference which I can think of are: > - Argument passing, a block isn't strict about the arguments > it gets passed, (i.e. it can take any argument). A Proc > object in contrast is strict (like methods). The reason I > can think of is that it is often usefull not to get all arguments > in a block, i.e. 3.times{ puts "Hi, world!" } > - The place where return goes to: in a block 'return' returns > from the method in which it is defined. In a lambda (Proc) > it returns from the lambda expression. Thanks for pointing these out; they didn't come to mind before. Gavin