%i
或%I
在Ruby中的含义是什么?
我在Google上搜索了一下
"%i or %I" ruby
但我没有找到与Ruby相关的任何内容。
%i
或%I
在Ruby中的含义是什么?
我在Google上搜索了一下
"%i or %I" ruby
但我没有找到与Ruby相关的任何内容。
%i[ ] # Non-interpolated Array of symbols, separated by whitespace
%I[ ] # Interpolated Array of symbols, separated by whitespace
我的搜索结果中的第二个链接是http://ruby.zigzo.com/2014/08/21/rubys-notation/
在IRB中的示例:
%i[ test ]
# => [:test]
str = "other"
%I[ test_#{str} ]
# => [:test_other]
%i %q %r %s %w %x
形式,每种都有一个大写版本以实现插值。 (请参见Ruby literals docs的解释)。[]
形式的分隔符,还可以使用任何类型的括号() {} [] <>
,并且您可以使用(引用自Ruby文档)“大多数其他非字母数字字符作为百分字符串分隔符,例如“%”、“| ”,“^”等等。”%i% bish bash bosh %
与%i[bish bash bosh]
的作用相同。这就像%w
和%W
,类似于'
和"
的用法:
x = :test
# %w won't interpolate #{...} style strings, leaving as literal
%w[ #{x} x ]
# => ["\#{x}", "x"]
# %w will interpolate #{...} style strings, converting to string
%W[ #{x} x ]
# => [ "test", "x"]
%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 ]
%I
。 - Mark Thomas%I
?你能详细说明一下吗? - Arup Rakshit