# Explanation of overriding method with lambda

**URL:** <https://www.ruby-forum.com/t/explanation-of-overriding-method-with-lambda/141619>\
**Category:** Ruby\
**Created:** [June 27, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/explanation-of-overriding-method-with-lambda/141619 "2008-06-27T20:20:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 27, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/explanation-of-overriding-method-with-lambda/141619/1 "2008-06-27T20:20:35Z")

</div>

Hi,

I am reading the tutorial at:  
[http://en.wikibooks.org/wiki/Programming:Ruby\_Method\_Calls](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

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 27, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/explanation-of-overriding-method-with-lambda/141619/2 "2008-06-27T21:01:35Z")

</div>

On Jun 27, 1:17 pm, Ruby F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 28, 2008, 9:40am UTC](https://www.ruby-forum.com/t/explanation-of-overriding-method-with-lambda/141619/3 "2008-06-28T09:40:14Z")

</div>

On Sat, Jun 28, 2008 at 1:47 AM, Ruby F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](http://ruby-smalltalk.blogspot.com/)

* * *

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

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 28, 2008, 1:50am UTC](https://www.ruby-forum.com/t/explanation-of-overriding-method-with-lambda/141619/4 "2008-06-28T01:50:46Z")

</div>

> 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
