Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJohn Fisher
7,974 Pointspush method on hash?
Is it possible to push items into a hash in a similar way you do with an array?
If so, I do i push an item into the key, and push into a value?
thanks
1 Answer
Andrew Stelmach
12,583 Pointshash = {}
hash["key"] = "value"
=> {"key" => "value"}
hash[:another_key] = :another_value
=> {"key" => "value", :another_key => :another_value}
hash.store(:yet_anotherkey, :yet_anothervalue)
=> {"key" => "value", :another_key => :another_value, :yet_anotherkey => :yet_anothervalue}
Couple of different methods there (the 'store' method is preferable sometimes for readability).
John Fisher
7,974 PointsJohn Fisher
7,974 PointsThanks Andrew.
What if i want to store stuff from within a function into a hash outside of the function?
I know that with push method and arrays, this will work, but with store and hashes, it doesnt work. (I know there is a name for this sort of thing, but i cant think of it right now... maybe global?)