1

I'm having an issue which I can't seem to solve. I have an array which I need to convert to an single string. The elements need to be put underneath each other.

sample_array = ['a','b','c','d','e']

desired output:

sample_array = "a
b
c
d
e"

I thought I could do this with a 'heredoc', but I can only get the elements behind each other inline. This is unfortunately not what I need. Anyone who can help me?

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Mitchell
  • 55
  • 5

1 Answers1

0

edit for edit question

In a single line, you can use inject:

sample_array = ['a','b','c','d','e']
puts sample_array.inject(""){|conc,x| conc + "\n" + x }

=> "a b c d e"

that will fold the array recursively and adding a line between chars

developer_hatch
  • 15,898
  • 3
  • 42
  • 75