Forum: Ruby nicer way to convert array of int to array of string

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Dc879bc4761b20d7897a5d439ea40d4e?d=identicon&s=25 S Kanakakorn (Guest)
on 2007-02-09 21:03
(Received via mailing list)

I'm quite new in Ruby.  Here is what I wrote to convert array of
integer to array of string.  I'm sure there is more compact and nicer
way to do this.  Can I see the "ruby" way here ?

def convert(intArray)
  stringArray = []
  intArray.each do |i|
    stringArray = stringArray + i.to_s.to_a
  return stringArray

C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2007-02-09 21:06
(Received via mailing list)
Alle venerdì 9 febbraio 2007, S Kanakakorn ha scritto:
>   end
>   return stringArray
> end
> Thanks,{|i| i.to_s}

map passes each value of the array to the block and puts what the block
returns in a array.

5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2007-02-09 21:27
(Received via mailing list)
On Sat, 2007-02-10 at 05:02 +0900, S Kanakakorn wrote:
>   end
>   return stringArray
> end

  [1, 2, 3, 4].map{|i| i.to_s } #=> ["1", "2", "3", "4"]

#map calls the given block sequentially with each item in the array, and
returns an array containing the values returned by those calls.

Daniel Schierbeck
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2007-02-09 21:30
(Received via mailing list)
On Feb 9, 9:05 pm, Stefano Crocco <> wrote:
> >   intArray.each do |i|
> returns in a array.
> Stefano

If you use Ruby on Rails or Ruby 1.9 you'll also be able to do this:
This topic is locked and can not be replied to.