views:

62

answers:

2

Hello!

I have a very small question.

How can I convert these:

[172592596,93038592,154137572]

To look like these:

['172592596','93038592','154137572']

Thankful for all help!

A: 

Try this!

b = []
a = [172592596,93038592,154137572]
a.each {|a1| b << a1.to_s}
b will return ["172592596", "93038592", "154137572"]

You can also use collect! same as map like @sepp2k suggested.

a = [172592596,93038592,154137572]
a.collect! {|x| x.to_s}
krunal shah
+10  A: 

If you want to turn an array of ints into an array of strings, you can do so easily using map and to_s.

arr = [172592596,93038592,154137572]
arr.map {|x| x.to_s}
#=> ["172592596", "93038592", "154137572"]

Since this is rails, you can also do (will also work in plain ruby if the version is at least 1.8.7):

arr.map(&:to_s)

To get the same result.

sepp2k
Thank you sepp2k!
Jonathan Clark
How can I convert ["333","444"] to [{"id":"333","id":"444"}?
Jonathan Clark
@Jonathan: `{"id":"333","id":"444"}` is a syntax error. You can't use string keys using the `:` syntax with string keys. If you change it `{"id" => "333", "id" => "444"}` it still doesn't work because you can't have duplicate keys in a hash.
sepp2k