日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Ruby]转载: 关于ruby中 %Q, %q, %W, %w, %x, %r, %s 的用法

發(fā)布時(shí)間:2025/7/14 编程问答 100 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Ruby]转载: 关于ruby中 %Q, %q, %W, %w, %x, %r, %s 的用法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

單引號(hào)內(nèi)的內(nèi)容,ruby會(huì)原樣輸出

雙引號(hào)內(nèi)的內(nèi)容,ruby會(huì)解析

我們看個(gè)簡單的例子,針對(duì)字符串 ? ???#{foo}test? ? ?我們分別用單引號(hào)核雙引號(hào)操作

'#{foo}test' => "\#{foo}test"

  

"#{foo}test" NameError: undefined local variable or?method `foo' for?main:Object ????????from (irb):4 ????????from C:/Ruby192/bin/irb:12:in?`<main>'

顯而易見, 雙引號(hào)對(duì)字符串內(nèi)的#進(jìn)行了解析, 導(dǎo)致ruby認(rèn)為foo是個(gè)方法,出錯(cuò).

類似的, %W %Q 相當(dāng)于雙引號(hào), ? %w %q 相當(dāng)于單引號(hào).

?

轉(zhuǎn)載:?http://simpleror.wordpress.com/2009/03/15/q-q-w-w-x-r-s/??

%Q

This is an alternative for double-quoted strings, when you have more quote characters in a string.Instead of putting backslashes in front of them, you can easily write:
>> %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

Used for single-quoted strings.The syntax is similar to?%Q, but single-quoted strings are not subject to expression substitution or escape sequences.
>> %q(Joe said: 'Frank said: '#{what_frank_said} ' ')
=> "Joe said: 'Frank said: '\#{what_frank_said} ' '"

?

%W

Used for double-quoted array elements.The syntax is similar to?%Q
>> %W(#{foo} Bar Bar\ with\ space)
=> ["Foo", "Bar", "Bar with space"]

?

%w

Used for single-quoted array elements.The syntax is similar to?%Q, but single-quoted elements are not subject to expression substitution or escape sequences.
>> %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

Used for regular expressions.The syntax is similar to?%Q.
>> %r(/home/#{foo})
=> "/\\/home\\/Foo/"

?

%s

Used for symbols.It’s not subject to expression substitution or escape sequences.
>> %s(foo)
=> :foo
>> %s(foo bar)
=> :"foo bar"
>> %s(#{foo} bar)
=> :"\#{foo} bar"

轉(zhuǎn)載于:https://www.cnblogs.com/buhaiqing/p/3537558.html

總結(jié)

以上是生活随笔為你收集整理的[Ruby]转载: 关于ruby中 %Q, %q, %W, %w, %x, %r, %s 的用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。