From: James Coglan Date: 2009-03-20T18:07:22+09:00 Subject: Re: Passing a named function instead of a code block? --001636c599095d9f6c04658948fb Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 2009/3/20 7stud -- > Matthias Reitinger wrote: > > > > puts c.collect(&method(:fib)) > > > > Why is there a difference here: > > def square1(x) > x*x > end > > square2 = lambda { |x| x*x} > > puts [1, 2, 3].collect(&square2) > puts [1, 2, 3].collect(&square1) > > --output:-- > 1 > 4 > 9 > r1test.rb:8:in `square1': wrong number of arguments (0 for 1) > (ArgumentError) > from r1test.rb:8 > > > Why does ruby make you use the tortured syntax: > > &method(:square1) > > for a method vs. the easier syntax for a Proc object? square2 is a variable name (ie. something you've made an assignment to), it's just a reference to the lambda object. However, square1 is a method and Ruby allows calling methods without parens, so 'square1' is actually interpreted as a method call to square1 with no arguments. Therefore, to grab a method as an object without calling it, we need to use method(:square1). -- James Coglan http://github.com/jcoglan --001636c599095d9f6c04658948fb--