54

This is my array:

array = [:one,:two,:three]

I want to apply to_s method to all of my array elements to get array = ['one','two','three'].

How can I do this (converting each element of the enumerable to something else)?

1

4 Answers 4

88

This will work:

array.map!(&:to_s)
Sign up to request clarification or add additional context in comments.

5 Comments

It works in Ruby 1.8.7 for sure. I use it in my projects all over.
Symbol#to_proc wasn't available in 1.8.6, but is available in 1.8.7: apidock.com/ruby/Symbol/to_proc . I know this from personal experience as well, as I don't use Rails or its libraries.
I figure it's worth noting that collect and collect! are aliases for map and map!.
what's the ! for?
@user1492534 ! is part of the method name, no magic, but it's used to tell you it defines the changed value to self instead of just returning the changed value: stackoverflow.com/a/612196/540447
20

You can use map or map! respectively, the first will return a new list, the second will modify the list in-place:

>> array = [:one,:two,:three]
=> [:one, :two, :three]

>> array.map{ |x| x.to_s }
=> ["one", "two", "three"]

1 Comment

Thanks for a quick and detailed answer!
17

It's worth noting that if you have an array of objects you want to pass individually into a method with a different caller, like this:

# erb
<% strings = %w{ cat dog mouse rabbit } %>
<% strings.each do |string| %>
  <%= t string %>
<% end %>

You can use the method method combined with block expansion behavior to simplify:

<%= strings.map(&method(:t)).join(' ') %>

If you're not familiar, what method does is encapsulates the method associated with the symbol passed to it in a Proc and returns it. The ampersand expands this Proc into a block, which gets passed to map quite nicely. The return of map is an array, and we probably want to format it a little more nicely, hence the join.

The caveat is that, like with Symbol#to_proc, you cannot pass arguments to the helper method.

Comments

10
  • array.map!(&:to_s) modifies the original array to ['one','two','three']
  • array.map(&:to_s) returns the array ['one','two','three'].

4 Comments

@sawa: Actually, that's not true. Ruby-On-Rails is actually a programming language, with more questions asked about it than about Ruby. But just like Perl, CLU and SmallTalk, Ruby has incorporated the best bits of it into its language. ;) xkcd.com/386
@Andrew Okay. Thanks. I removed my comment right before you gave some.
For those wondering what I was humorously replying to, @sawa basically asked if the answerer knew that Ruby is a language, and that Rails is a framework that runs on Ruby, because @sawa thought that Symbol#to_proc was Rails-only in 1.8.7.
@AndrewGrimm: no, Ruby on Rails is not a programming language.

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.