Create array of symbols

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

    %i[ ] Non-interpolated Array of symbols, separated by whitespace (after Ruby 2.0)

    %I[ ] Interpolated Array of symbols, separated by whitespace (after Ruby 2.0)

    %i[address city state postal country]

    the cleanest way to do this is:

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

提交回复
热议问题