Create array of symbols

前端 未结 3 1126
醉酒成梦
醉酒成梦 2021-01-30 03:22

Is there a cleaner way to do something like this?

%w[address city state postal country].map(&:to_sym) 
#=> [:address, :city, :state, :postal, :country]
         


        
3条回答
  •  忘掉有多难
    2021-01-30 04:03

    With a risk of becoming too literal, I think the cleanest way to construct an array of symbols is using an array of symbols.

    fields = [:address, :city, :state, :postal, :country]
    

    Can't think of anything more concise than that.

提交回复
热议问题