3

I would like to replace this:

[[1,'a'],[2,'b'],[3,'c']].map {|p| p.first}

with a more terse version by sending a reference to Array's first method to map, but it gives me errors:

[[1,'a'],[2,'b'],[3,'c']].map Array.method(:first)
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
akonsu
  • 28,824
  • 33
  • 119
  • 194

1 Answers1

3

Try this:

[[1,'a'],[2,'b'],[3,'c']].map(&:first)
seph
  • 6,066
  • 3
  • 21
  • 19
  • 1
    It's called the "pretzel colon" and is a shorthand for `Symbol#to_proc`. http://stackoverflow.com/questions/1217088/what-does-mapname-mean-in-ruby – Jared Beck Aug 08 '12 at 04:29