像PHP nowdoc这样的Ruby多行字符串?

时间:2014-02-24 15:22:42

标签: ruby

可以像Ruby的nowdoc [1]

那样在Ruby中使用多行字符串

e.g。

puts '

\\foo

'

我想输出以下没有逃避的内容

\\foo

[1] Nowdocs是单引号字符串,heredocs是双引号字符串。类似于heredoc指定了nowdoc,但是在nowdoc内部没有进行解析。 http://hk1.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

3 个答案:

答案 0 :(得分:4)

literals documentation中所述,您只需在heredoc标识符周围加上单引号,如下所示:

puts <<'EOS'
#{variable}
\\escaped
EOS

输出:

#{variable}
\\escaped

答案 1 :(得分:1)

在heredoc分隔符周围加上单引号:

> tmp = "hi"
> s = <<'EOS'
' #{tmp}
' \\foo
' EOS
=> "\#{tmp}\n\\\\foo\n"
> puts s
#{tmp}
\\foo

答案 2 :(得分:0)

2.1.0 :001 > %q{
2.1.0 :002'> my
2.1.0 :003'> single
2.1.0 :004'> quoted
2.1.0 :005'> multiline
2.1.0 :006'> string
2.1.0 :007'> }
 => "\nmy\nsingle\nquoted\nmultiline\nstring\n" 

2.1.0 :008 > %Q{
2.1.0 :009"> my
2.1.0 :010"> double
2.1.0 :011"> quoted
2.1.0 :012"> multiline
2.1.0 :013"> string
2.1.0 :014"> }
 => "\nmy\ndouble\nquoted\nmultiline\nstring\n"

更多信息http://simpleror.wordpress.com/2009/03/15/q-q-w-w-x-r-s/

相关问题