From: Tamara Temple Date: 2013-07-21T15:46:15+09:00 Subject: Re: ActiveSupport undercore behaviour On Jul 20, 2013, at 2:51 PM, Serguei Cambour wrote: > I tried to use ActiveSupport 'underscore' method to convert some > non-conventional to RoR data to be able to update later some attributes > as follows: > > TECHS = {"platform"=>[ > {"AssignedName"=>"P1", "ExportPrice"=>"USD", "Id"=>"1", > "Delivery"=>"Y", > "RegisteredMembers"=>[ > {"Name"=>"Admin", "Id"=>"3", > "AvailablePosts"=>[ > {"Seat"=>"2", "Colour"=>18, }, > {"Seat"=>"3", "Colour"=>19, }, > {"Seat"=>"4", "Colour"=>181, }, > {"Seat"=>"5", "Colour"=>183, } > ] > } > ] > } > ] > } > > TECHS.each_value do |value| > arr = value.map {|k,v| [ActiveSupport::Inflector.underscore(k.to_s), > v]} > puts "ARR: #{arr.inspect}" > end > > The result is strange even if the values look underscored: > > ARR: [["{\"assigned_name\"=>\"p1\", \"export_price\"=>\"usd\", > \"id\"=>\"1\", \"delivery\"=>\"y\", > \"registered_members\"=>[{\"name\"=>\"admin\", \"id\"=>\"3\", > \"available_posts\"=>[{\"seat\"=>\"2\", \"colour\"=>18}, > {\"seat\"=>\"3\", \"colour\"=>19}, {\"seat\"=>\"4\", \"colour\"=>181}, > {\"seat\"=>\"5\", \"colour\"=>183}]}]}", nil]] > > Why are there some extra quotes and nil in the very end ? Thkx > > -- > Posted via http://www.ruby-forum.com/. > Not 100% sure what you're seeing as extra quotes, but the nil is what you get from declaring two passed in values from the map method -- it only passes in the current value of the array that your original TECH['platform'] is pointing at, so k is current value, v is always nil. 2.0.0-p247 :008 > [1, 2, 3, 4].map{|k,v| [k,v]} => [[1, nil], [2, nil], [3, nil], [4, nil]] Even if your array elements are hashes, there's still only one value passed in from map: 2.0.0-p247 :009 > [{a: 1, b: 2, c: 3}, {aa: 11, bb: 22, cc: 33}].map{|k,v| [k,v]} => [[{:a=>1, :b=>2, :c=>3}, nil], [{:aa=>11, :bb=>22, :cc=>33}, nil]]