Create array of symbols

前端 未结 3 1073
醉酒成梦
醉酒成梦 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:25

    The original answer was written back in September '11, but, starting from Ruby 2.0, there is a shorter way to create an array of symbols! This literal:

    %i[address city state postal country]
    

    will do exactly what you want.

提交回复
热议问题