尝试在 Ruby 中格式化多行字符串
heredoc
和 %q{ }
的问题是它们包含用于格式化代码的空格。
s = %q{Foo
Bar
Baz}
puts s
错误地输出以下内容:
Foo
Bar
Baz
以下是可行的,但是对于 \
字符来说有点难看。
s = "Foo\n" \
" Bar\n" \
" Baz"
puts s
以下在 python 中有效:
s = ("Foo\n"
" Bar\n"
" Baz")
print s
Ruby 中是否有等效项?
最佳答案
构建没问题,但比我想的要危险得多
s = %w{ Foo
Bar
Baz}
puts s
=>
Foo
Bar
Baz
如果你想保留第一行的缩进,这肯定是设计好的
s = <<-END
Foo
Bar
Baz
END
puts s
=>
Foo
Bar
Baz
关于ruby - 多行字符串格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10113043/