[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Explanation of overriding method with lambda

Ruby Freak

6/27/2008 6:16:00 PM

Hi,

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

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





3 Answers

Yossef Mendelssohn

6/27/2008 6:59:00 PM

0

On Jun 27, 1:17 pm, Ruby Freak <twscann...@gmail.com> 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.

--
-yossef

Ruby Freak

6/27/2008 11:45:00 PM

0


> 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.

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

Robert Dober

6/28/2008 7:37:00 AM

0

On Sat, Jun 28, 2008 at 1:47 AM, Ruby Freak <twscannell@gmail.com> 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.blo...

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