How to compute 'map' in Ruby without using blocks?

I know I can do this in Ruby:

['a', 'b'].map do |s| s.to_sym end

and get this:

[:a, :b]

I'm looking for a more concise way to do it, without using a block. Unfortunately, this doesn't work:

['a', 'b'].map #to_sym

Can I do better than with the initial code?

-------------Problems Reply------------

Read a bit about Symbol#to_proc:

['a', 'b'].map(&:to_sym)
# or
['a', 'b'].map &:to_sym
# Either will result in [:a, :b]

This works if you're using Ruby 1.8.7 or higher, or if you're using Rails - ActiveSupport will add this functionality for you.

['a', 'b'].map(&:to_sym) is shorter

Category:ruby Views:8 Time:2011-09-05
Tags: ruby

Related post

Copyright (C), All Rights Reserved.

processed in 0.197 (s). 11 q(s)