符号数组是否有文字表示法? [英] Is there a literal notation for an array of symbols?

查看:41
本文介绍了符号数组是否有文字表示法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢这个字符串数组的文字表达式:

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(foo bar)%i!foo bar! 例如.

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:

http://www.ruby-lang.org/zh_TW/news/2012/11/02/ruby-2-0-0-preview1-released/

Ruby 官方文档中提到:

It is mentioned in the official documentation of Ruby here:

http://ruby-doc.org/core/doc/syntax/literals_rdoc.html#label-Percent+Strings

这篇关于符号数组是否有文字表示法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆