Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails %i或%i在ruby中做什么?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails %i或%i在ruby中做什么?

Ruby on rails %i或%i在ruby中做什么?,ruby-on-rails,ruby,Ruby On Rails,Ruby,在ruby中%i或%i是什么意思 我在谷歌上搜索 "%i or %I" ruby 但是没有发现任何与ruby有关的东西 %i[ ] # Non-interpolated Array of symbols, separated by whitespace %I[ ] # Interpolated Array of symbols, separated by whitespace 我的搜索结果中的第二个链接 IRB中的示例: %i[ test ] # => [:test] str = "o

在ruby中%i或%i是什么意思

我在谷歌上搜索

"%i or %I" ruby
但是没有发现任何与ruby有关的东西

%i[ ] # Non-interpolated Array of symbols, separated by whitespace
%I[ ] # Interpolated Array of symbols, separated by whitespace
我的搜索结果中的第二个链接

IRB中的示例:

%i[ test ]
# => [:test]
str = "other"
%I[ test_#{str} ]
# => [:test_other] 

这就像
%w
%w
一样,它们的工作原理类似于

现在,
%i
%i
的情况也一样:

# %i won't interpolate #{...} style strings, leaving as literal, symbolized
%i[ #{x} x ]
# => [:"\#{x}", :x ]

# %w will interpolate #{...} style strings, converting to symbols
%I[ #{x} x ]
# => [ :test, :x ]

很难找到正式的Ruby文档()。在编写当前版本时,它是2.5.1,而%i构造的文档可以在的文档中找到

Ruby的%construct有一些令人惊讶的变体(至少对我来说是如此!)。有一些常用的
%i%q%r%s%w%x
表单,每个表单都有一个大写版本来启用插值。(有关解释,请参阅)

但您可以使用多种类型的分隔符,而不仅仅是
[]
。您可以使用任何类型的括号
(){}[]
,并且您可以使用(引用ruby文档)“大多数其他非字母数字字符作为百分比字符串分隔符,例如“%”、“|”、“^”等。”


因此
%i%bish bash bosh%
的工作原理与
%i[bish bash bosh]的工作原理相同

aka这是正确的。但是我不建议使用
%I
。@MarkThomas为什么不建议使用
%I
,请详细说明一下?@ArupRakshit我的猜测是,它很难看,可能比使用更明显的字符串插值更不清晰。这更清楚地回答了问题。“%I”的用法“只是从由空格分隔的字符串数组中创建散列键。非常感谢您的回答。@VishalKanaujia虽然符号通常用作散列键,但这不是它们的唯一用途。在Ruby中,散列键可以是任何东西,符号可以在许多其他情况下使用。仅供参考,方括号的调用。
# %i won't interpolate #{...} style strings, leaving as literal, symbolized
%i[ #{x} x ]
# => [:"\#{x}", :x ]

# %w will interpolate #{...} style strings, converting to symbols
%I[ #{x} x ]
# => [ :test, :x ]