209

I have a ruby array like ['12','34','35','231'].

I want to convert it to a string like '12','34','35','231'.

How can I do that?

13 Answers 13

358

I'll join the fun with:

['12','34','35','231'].join(', ')
# => 12, 34, 35, 231

EDIT:

"'#{['12','34','35','231'].join("', '")}'"
# => '12','34','35','231'

Some string interpolation to add the first and last single quote :P

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

5 Comments

That results in "12,34,35,231". It's missing the single quotes in the result.
Okay added some string interpolation to add the first and last single quotes :P
how to revert this?
what do you mean revert @zx1986
@corroded Sorry, I got it. JSON.parse("[12, 39, 100]") will return an array.
46
> a = ['12','34','35','231']
> a.map { |i| "'" + i.to_s + "'" }.join(",")
=> "'12','34','35','231'"

2 Comments

Perhaps using "'#{i}'" instead.
don't think map is needed. join should do the trick. see below
36

try this code ['12','34','35','231']*","

will give you result "12,34,35,231"

I hope this is the result you, let me know

1 Comment

I think the OP needs to have the single quote as well.
12
array.map{ |i|  %Q('#{i}') }.join(',')

2 Comments

To quote Mladen, "Perhaps [use] "'#{i}'" instead."
This is the right way to do it, should be the accepted answer.
9
string_arr.map(&:inspect).join(',') # or other separator

4 Comments

This does not produce the correct output - the values needs to be wrapped in single quotes. If this was the desired output then string_arr.join(",") would be the better option.
Sean, you're wrong. Did you run the expression, at least once ??
It's still wrong. It results in double quotes around the array entries, not single quotes. Plus it relies upon an assumption about the format that "inspect()" prints data, which makes it fragile.
['1','2','3'].map { |o| "\'#{o}\'" }.join(',')
7

I find this way readable and rubyish:

add_quotes =- > x{"'#{x}'"}

p  ['12','34','35','231'].map(&add_quotes).join(',') => "'12','34','35','231'"

Comments

5
> puts "'"+['12','34','35','231']*"','"+"'"
'12','34','35','231'

> puts ['12','34','35','231'].inspect[1...-1].gsub('"',"'")
'12', '34', '35', '231'

Comments

4

And yet another variation

a = ['12','34','35','231']
a.to_s.gsub(/\"/, '\'').gsub(/[\[\]]/, '')

Comments

3
irb(main)> varA
=> {0=>["12", "34", "35", "231"]}
irb(main)> varA = Hash[*ex.collect{|a,b| [a,b.join(",")]}.flatten]
...

Comments

3
irb(main):027:0> puts ['12','34','35','231'].inspect.to_s[1..-2].gsub('"', "'")
'12', '34', '35', '231'
=> nil

Comments

2

You can use some functional programming approach, transforming data:

['12','34','35','231'].map{|i| "'#{i}'"}.join(",")

Comments

2

suppose your array :

arr=["1","2","3","4"]

Method to convert array to string:

Array_name.join(",")

Example:

arr.join(",")

Result:

"'1','2','3','4'"

Comments

-6

array.inspect.inspect.gsub(/\[|\]/, "") could do the trick

Comments

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.