是否有符号的数组的文字符号? [英] Is there a literal notation for an array of symbols?
问题描述
我喜欢一个字符串数组这个文字前pression:
I like this literal expression for an array of strings:
%w( i can easily create arrays of words )
我想知道,如果有一个文字得到符号的数组。我知道我可以做
I am wondering if there is a literal to get an array of symbols. I know I can do
%w( it is less elegant to create arrays of symbols ).map( &:to_sym )
但它是如此美妙只使用文字。
but it would be so wonderful just to use a literal.
推荐答案
是的!这是现在可以用Ruby 2.0.0。把它写的一个方法是:
Yes! This is possible now in Ruby 2.0.0. One way to write it is:
%i{foo bar} # => [:foo, :bar]
您也可以使用其他的分隔符,所以你也可以写%I(富吧)
或%我!富吧!
例如
You can also use other delimiters, so you could also write %i(foo bar)
or %i!foo bar!
for example.
该功能最初是在此间宣布:
This feature was originally announced here:
<一个href=\"http://www.ruby-lang.org/zh_TW/news/2012/11/02/ruby-2-0-0-$p$pview1-released/\">http://www.ruby-lang.org/zh_TW/news/2012/11/02/ruby-2-0-0-$p$pview1-released/
据这里的Ruby的官方文档中提到的:
It is mentioned in the official documentation of Ruby here:
<一个href=\"http://ruby-doc.org/core/doc/syntax/literals_rdoc.html#label-Percent+Strings\">http://ruby-doc.org/core/doc/syntax/literals_rdoc.html#label-Percent+Strings
这篇关于是否有符号的数组的文字符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!