Explanation of overriding method with lambda

Hi,

I am reading the tutorial at:
http://en.wikibooks.org/wiki/Programming:Ruby_Method_Calls

and I understand everything except this last method override code.

We had this,(line below) which is simple and we are overriding to make
it “better”
upcase_words = words.map {|x| x.upcase}

class Symbol
# A generalized conversion of a method name
# to a proc that runs this method.
#
def to_proc
lambda {|x, *args| x.send(self, *args)}
end
end

Voila !

words = %w(Jane, aara, multiko)
upcase_words = words.map(&:upcase)

So we are overriding the to_proc method with
lambda {|x, *args| x.send(self, *args)}

so in the lambda, the x in |x, args| is the same as the |x| in the
block (Jane, aara, multiko)(yes?, no?)

and the args in |x, args| is the symbol :ucase, (yes?, no?)

I don’t understand x.send(self, *args)
self is the symbol :ucase ?
but so is args ???

In my brain, at some point I get “jane.send(self, *args)”

I’m lost.

I do understand the concept of opening a class and overriding a
method, just not the details here.

Thank in advance

On Jun 27, 1:17 pm, Ruby F. [email protected] wrote:

words = %w(Jane, aara, multiko)
upcase_words = words.map(&:upcase)

So we are overriding the to_proc method with
lambda {|x, *args| x.send(self, *args)}

so in the lambda, the x in |x, args| is the same as the |x| in the
block (Jane, aara, multiko)(yes?, no?)

Yes

and the args in |x, args| is the symbol :ucase, (yes?, no?)

No

I don’t understand x.send(self, *args)
self is the symbol :ucase ?
but so is args ???

In my brain, at some point I get “jane.send(self, *args)”

I’m lost.

So you know that & is a shorthand for saying “this is a block” and it
converts the following value using its to_proc method, right? That’s
where the convenient shorthand of &:upcase comes from. Look at these
snippets:

%w[one two three].collect { |x| x.upcase }
%w[one two three].collect { |x| x.send(:upcase) } # same as above

class Symbol
def to_proc
lambda { |x| x.send(self) }
end
end

%w[one two three].collect(&:upcase) # calls :upcase.to_proc
:upcase.to_proc returns lambda { |x| x.send(:upcase) }

I don’t quite understand when I see the definition of Symbol#to_proc
with *args. Where might these args come from? Maybe I’m just not using
Symbol#to_proc correctly.

Try this on for size:

class Symbol
def to_proc
lambda do |x, *args|
p x
p self
p args
x.send(self, *args)
end
end
end

%w[one two three].collect(&:upcase)

(output)
“one”
:upcase
[]
“two”
:upcase
[]
“three”
:upcase
[]
=> [“ONE”, “TWO”, “THREE”]

Hope this helped.

On Sat, Jun 28, 2008 at 1:47 AM, Ruby F. [email protected]
wrote:

Thanks yossef. The override you wrote makes sense. I give up on *args

I think that is a good decision as they cannot be accessed at all.
This seems like an error on the Wiki page to me.

Cheers
Robert


http://ruby-smalltalk.blogspot.com/


AALST (n.) One who changes his name to be further to the front
D.Adams; The Meaning of LIFF

with *args. Where might these args come from? Maybe I’m just not using
Symbol#to_proc correctly.

That is/was exactly my problem, I don’t understand the *args part.

Thanks yossef. The override you wrote makes sense. I give up on *args