Ruby: 字符串与 "\n" 在一行中返回

Ruby: strings concated with "\n" returned in one line

我有这个代码:

def rectangle
  "|------------------|\n" +
  "|                  |\n" +
  "|                  |\n" +
  "|                  |\n" +
  "|                  |\n" +
  "|                  |\n" +
  "|                  |\n" +
  "|                  |\n" +
  "|                  |\n" +
  "|------------------|\n"
  end

我想重构它。但出于某种原因,如果我尝试以任何其他方式 concat/merge 字符串 "\n" 停止工作,它会在一行中返回。

def rectangle
    a = "|------------------|\n"
    b = "|                  |\n"
    a + b + a
end

我试过使用

System.getProperty("line.separator", "\n")

如类似帖子中所建议的那样,但这没有帮助(或者我做得不对)。这是我课程的一部分。感觉就像我错过了一些明显的东西。

一个简单的例子是:

def rectangle
  a = "|#{'-' * 10}|"
  b = "|#{' ' * 10}|"
  ([a] + [b] * 5 + [a]).join("\n")
end

puts rectangle