53

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)?

lulalala
  • 17,572
  • 15
  • 110
  • 169
Vladimir Tsukanov
  • 4,269
  • 8
  • 28
  • 34
  • Possible duplicate of [Convert an array of integers into an array of strings in Ruby?](http://stackoverflow.com/questions/781054/convert-an-array-of-integers-into-an-array-of-strings-in-ruby) – Nakilon Oct 03 '15 at 17:49

4 Answers4

86

This will work:

array.map!(&:to_s)
sawa
  • 165,429
  • 45
  • 277
  • 381
  • 1
    It works in Ruby 1.8.7 for sure. I use it in my projects all over. – Alex Kovshovik Jun 27 '11 at 21:27
  • Symbol#to_proc wasn't available in 1.8.6, but is available in 1.8.7: http://apidock.com/ruby/Symbol/to_proc . I know this from personal experience as well, as I don't use Rails or its libraries. – Andrew Grimm Jun 27 '11 at 23:17
  • 1
    I figure it's worth noting that `collect` and `collect!` are aliases for `map` and `map!`. – taylorthurlow Oct 24 '17 at 17:59
  • what's the `!` for? – Ruslan López Apr 25 '18 at 04:42
  • @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: https://stackoverflow.com/a/612196/540447 – Tim Baas May 31 '18 at 21:07
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"]
miku
  • 181,842
  • 47
  • 306
  • 310
18

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.

coreyward
  • 77,547
  • 20
  • 137
  • 166
9
  • array.map!(&:to_s) modifies the original array to ['one','two','three']
  • array.map(&:to_s) returns the array ['one','two','three'].
Arun Kumar Arjunan
  • 6,827
  • 32
  • 35
  • @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. ;) http://xkcd.com/386/ – Andrew Grimm Jun 27 '11 at 23:23
  • @Andrew Okay. Thanks. I removed my comment right before you gave some. – sawa Jun 27 '11 at 23:26
  • 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. – Andrew Grimm Jun 27 '11 at 23:38
  • 1
    @AndrewGrimm: no, Ruby on Rails is not a programming language. – iconoclast Jan 04 '23 at 20:40