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
  • 155,156
  • 41
  • 207
  • 295
akonsu
  • 27,402
  • 26
  • 115
  • 185

1 Answers1

3

Try this:

[[1,'a'],[2,'b'],[3,'c']].map(&:first)
seph
  • 5,928
  • 2
  • 20
  • 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