21

I need to convert fixnums to strings. My solution is:

arr.map {|a| a.to_s}

Is there a better way?

1
  • 2
    Your solution is about as clear (good) as it gets, IMHO. Commented Nov 30, 2010 at 23:07

1 Answer 1

44
arr.map(&:to_s)

This uses a spiffy new feature in Ruby >= 1.8.7, the "symbol to proc" shortcut, and is equivalent to the code in your question.

Sign up to request clarification or add additional context in comments.

3 Comments

I hadn't seen this before. What is the operator called? Do you have a link to documentation for the feature?
@hambend, Great questions. Answer amended.
BTW: while this was only added to the Ruby core library in 1.9 and backported to 1.8.7, it has been available in ActiveSupport, Facets and many other Ruby extension libraries for ages before that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.