Is there a literal notation for an array of symbols?

后端 未结 2 743
梦谈多话
梦谈多话 2020-12-12 18:26

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 g

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 19:12

    Yes! This is possible now in Ruby 2.0.0. One way to write it is:

    %i{foo bar}  # => [: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/

    It is mentioned in the official documentation of Ruby here:

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

提交回复
热议问题