Map with &:method_name

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

On Jul 10, 2008, at 3:11 PM, Oliver S. 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

a @ http://codeforpeople.com/

On Thu, Jul 10, 2008 at 5:22 PM, ara.t.howard [email protected]
wrote:

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.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

ara.t.howard wrote:

On Jul 10, 2008, at 3:11 PM, Oliver S. 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs