Map with &:method_name

R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

On Jul 10, 2008, at 3:11 PM, Oliver Saunders wrote:

I see this a lot but it doesn't work for me in IRB. Is a gem required or
something?

[1,2,3].map(&:to_s)
TypeError: wrong argument type Symbol (expected Proc)
from (irb):2


it's rails - basically


class Symbol
def to_proc
sym = self
lambda{|obj| obj.send sym}
end
end

And it's been picked up in Ruby 1.9, and maybe 1.8.7 as well since the
current draft of the Pickaxe 3rd ed doesn't label it as 1.9 only.
 
O

Oliver Saunders

ara.t.howard said:
I see this a lot but it doesn't work for me in IRB. Is a gem
required or
something?
[1,2,3].map(&:to_s)
TypeError: wrong argument type Symbol (expected Proc)
from (irb):2


it's rails - basically


class Symbol
def to_proc
sym = self
lambda{|obj| obj.send sym}
end
end

a @ http://codeforpeople.com/

OK thanks guys. I found the actual definition:

class Symbol
def to_proc
Proc.new { |*args| args.shift.__send__(self, *args) }
end
end
 

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
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top