Book creator (disable)

Talk:Ruby Programming/Syntax/Method Calls

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] The ampersand (&)

I'm starting to learn Ruby and I think there is a mistake in the section about the ampersand. The redefinition of to_proc in class Symbol defined as

def to_proc
   lambda {|x, *args| x.send(self, *args)}
end

doesn't work in my Ruby version 1.8.

What works is this:

def to_proc
   lambda {|*args| self.send(self, *args)}
end

I am not sure if the first version worked in any of the previous releases of Ruby?

I would like to change it, but I would really appreciate someone checking if the new code is really 100% correct.

--filu 17:51, 7 April 2007 (UTC)

OK, the mistake is mine. The version of to_proc method found in the article indeed works, for method calls such as is the case with the upcase. However my version works for function calls, such as

def fun_upcase( s )
   s.upcase
end

which in turn cannot be combined with the original to_proc.

Maybe this should be clarified in the text of the article.

--filu 19:31, 7 April 2007 (UTC)