有没有更干净的方法来做这样的事情?
%w[address city state postal country].map(&:to_sym)
#=> [:address, :city, :state, :postal, :country]
我本以为%s
能满足我的要求,但事实并非如此.它只是把所有的东西都放在括号里,然后用它做成一个大符号.
只是个小麻烦.
有没有更干净的方法来做这样的事情?
%w[address city state postal country].map(&:to_sym)
#=> [:address, :city, :state, :postal, :country]
我本以为%s
能满足我的要求,但事实并非如此.它只是把所有的东西都放在括号里,然后用它做成一个大符号.
只是个小麻烦.
最初的答案写在9月11日,但是,starting from Ruby 2.0,有一个更短的方法来创建一个符号数组!以下文字:
%i[address city state postal country]
会做你想做的事.