deleting blank lines after loop

后端 未结 3 705
滥情空心
滥情空心 2021-01-25 19:51

ok, I have the following, very simple code:

f = \"String1\\n\\n\\nString2\\n\\n\\n\"
f.each_line do |t|
  t.delete! \"\\n\"
  puts t.inspect
end
<
3条回答
  •  忘了有多久
    2021-01-25 20:01

    f = "String1\n\n\nString2\n\n\n"
    f.each_line.collect(&:chomp).reject(&:empty?)
    #=> ["String1", "String2"]
    

    The collect(&:chomp) removes line endings. reject(&:empty?) throws away all of the empty lines.

提交回复
热议问题