Explanation of overriding method with lambda

R

Ruby Freak

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
 
Y

Yossef Mendelssohn

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

Ruby Freak

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
 
R

Robert Dober

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top