加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在ruby字符串分隔符中的’%{}’,’%Q {}’,’%q {}’之间的差

发布时间:2020-12-16 19:25:59 所属栏目:百科 来源:网络整理
导读:我正在通过一个关于ruby的在线教程,发现这个“一般分隔字符串”, %{a word} # = "a word"%Q{a word} # = "a word"%q{a word} # equivalent to single quoted version. 所以我尝试了irb,这是我看到的 2.0.0p247 :025 %Q(hi) = "hi" 2.0.0p247 :026 %q(the) =
我正在通过一个关于ruby的在线教程,发现这个“一般分隔字符串”,
%{a word}  # => "a word"
%Q{a word} # => "a word"
%q{a word} # equivalent to single quoted version.

所以我尝试了irb,这是我看到的

2.0.0p247 :025 > %Q(hi)
 => "hi" 
2.0.0p247 :026 > %q(the)
 => "the" 
2.0.0p247 :027 > %q(th"e)
 => "th"e" 
2.0.0p247 :028 > %q(th'e)
 => "th'e" 
2.0.0p247 :029 > %Q(h'i)
 => "h'i" 
2.0.0p247 :030 > %Q(h"i)
 => "h"i"

%q和%Q都表现相同,并以双引号括起来.任何人知道如何使用这些2,如果我们可以使用%{}得到相同的输出.

解决方法

这里有一些关于他们的提示 Ruby_Programming - The % Notation

%Q[ ] – Interpolated String (default)

%q[ ] – Non-interpolated String (except for ,[ and ])

示例:

x = "hi"
p %Q[#{x} Ram!] #= > "hi Ram!"
p %q[#{x} Ram!] #= > "#{x} Ram!"
p %Q[the] #= > "the"
p %q[the] #= > "the" # notice the  with %q[]

另一个好的资源Percent Strings

Besides %(...) which creates a String,The % may create other types of object. As with strings,an uppercase letter allows interpolation and escaped characters while a lowercase letter disables them.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读