庇护祝福的分享

Be worthy

%Q, %q, %W, %w, %x, %r, %s

%Q

遇到引号嵌套的时候我们往往需要使用反斜线来实现转义,使用%Q(…)可以实现同样的效果,最终输出字符串,并且支持使用#{}操作变量

>> %Q(Joe said: "Frank said: "#{what_frank_said}"")
=> "Joe said: "Frank said: "Hello!"""

The parenthesis “(…)” can be replaced with any other non-alphanumeric characters and non-printing characters (pairs), so the following commands are equivalent:

>> %Q!Joe said: "Frank said: "#{what_frank_said}""!
>> %Q[Joe said: "Frank said: "#{what_frank_said}""]
>> %Q+Joe said: "Frank said: "#{what_frank_said}""+

You can use also:

>> %/Joe said: "Frank said: "#{what_frank_said}""/
=> "Joe said: "Frank said: "Hello!"""

%q

%q和%Q大同小异,只是用于单引号,因此无法支持变量

>> %q(Joe said: 'Frank said: '#{what_frank_said} ' ')
=> "Joe said: 'Frank said: '\#{what_frank_said} ' '"

%W

转化为数组并支持变量

>> %W(#{foo} Bar Bar\ with\ space)
=> ["Foo", "Bar", "Bar with space"]

%w

转化为数组,但不支持变量

>> %w(#{foo} Bar Bar\ with\ space)
=> ["\#{foo}", "Bar", "Bar with space"]

%x

Uses the ` method and returns the standard output of running the command in a subshell.The syntax is similar to %Q.

>> %x(echo foo:#{foo})
=> "foo:Foo\n"

%r

转化为正则表达式

>> %r(/home/#{foo})
=> "/\\/home\\/Foo/"

%s

转化为symbols.It’s not subject to expression substitution or escape sequences.

>> %s(foo)
=> :foo
>> %s(foo bar)
=> :"foo bar"
>> %s(#{foo} bar)
=> :"\#{foo} bar"