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

···

--
Posted via http://www.ruby-forum.com/\.

it's rails - basically

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

a @ http://codeforpeople.com/

···

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

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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.

···

On Thu, Jul 10, 2008 at 5:22 PM, ara.t.howard <ara.t.howard@gmail.com> wrote:

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

--
Rick DeNatale

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

ara.t.howard wrote:

···

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

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

--
Posted via http://www.ruby-forum.com/\.