ruby on rails - push new element into array within hash -
i have hash which, have keys uniquely identify each element within hash. , within each element, have array. question is, how put element inside array within hash.
{"apple"=>[1, 5.99], "banana"=>[5, 9.99]}
i'm looping through result set, , i'm little bit lost how add element array...
any appreciated
cheers
if hash called, example, hsh
, "apple" array can accessed hsh["apple"]
. can use variable, add value array hsh["apple"] << some_value
. so:
irb> hsh = { "apple" => [1, 5.99], "banana" => [5, 9.99] } irb> hsh["apple"] << 9999 => { "apple" => [1, 5.99, 9999], "banana" => [5, 9.99] }
Comments
Post a Comment