Array.join(“\n”) not the way to join with a newline?

后端 未结 7 1558
遥遥无期
遥遥无期 2020-12-29 01:44

I have an array..

[1,2,3,4]

and I want a string containing all the elements separated by a newline..

1

2

3

4
         


        
7条回答
  •  抹茶落季
    2020-12-29 02:35

    Yes, but if you print that string out it will have newlines in it:

    irb(main):001:0> a = (1..4).to_a
    => [1, 2, 3, 4]
    irb(main):002:0> a.join("\n")
    => "1\n2\n3\n4"
    irb(main):003:0> puts a.join("\n")
    1
    2
    3
    4
    

    So it does appear to achieve what you desire (?)

提交回复
热议问题