DMH DMH - 1 year ago 111
Ruby Question

split string without brackets and ensure quotes around each word

I have strings in the following format

as an example. What I need is these to be outputted in the following format
'one', 'word', 'after', 'another'

I have tried
but the output adds brackets which I dont need.

=> ["one", "world", "after", "another"]

I know this is because its an Array.

Can anyone recommend a way to achieve the outcome
'one', 'word', 'after', 'another'

Answer Source

Something like this should work:

puts 'one-world-after-another'.split("-").map {|e| "'#{e}'"}.join(", ")

This produces:

'one', 'world', 'after', 'another'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download