/i'm new to ruby on rails,
I'll like to know when to use Hash and when to use an Array. It seems confusing to me.
Thanks
When to use a hash versus an array?
Page 1 of 14 Replies - 1655 Views - Last Post: 27 December 2016 - 11:56 AM
Replies To: When to use a hash versus an array?
#2
Re: When to use a hash versus an array?
Posted 23 December 2016 - 09:58 PM
One is a list, the other is a set of mappings. If you want to give me a list of fruits, you would use an array, like so: ["apple", "banana", "orange"]. If you want to give me an association of fruits and some fact about each fruit, you would use a hash: {"apple": "red", "banana": "yellow", "orange": "orange"}
Hashes are typically used when you want to pass some structured data around, lists when the data does not have any appreciable internal structure (apart from, perhaps, its order)
Due to the way hashes manage the key->value mapping, a hash typically does not retain the order of insertion. (so it's an unordered list of relations)
(Note: This is a pretty language-neutral answer, since these structures exist in all modern languages, and also in PHP.)
(also note that I've changed your topic title to be a little more helpful. you're welcome)
Hashes are typically used when you want to pass some structured data around, lists when the data does not have any appreciable internal structure (apart from, perhaps, its order)
Due to the way hashes manage the key->value mapping, a hash typically does not retain the order of insertion. (so it's an unordered list of relations)
(Note: This is a pretty language-neutral answer, since these structures exist in all modern languages, and also in PHP.)
(also note that I've changed your topic title to be a little more helpful. you're welcome)
#3
Re: When to use a hash versus an array?
Posted 27 December 2016 - 04:41 AM
jon.kiparsky, on 23 December 2016 - 09:58 PM, said:
One is a list, the other is a set of mappings. If you want to give me a list of fruits, you would use an array, like so: ["apple", "banana", "orange"]. If you want to give me an association of fruits and some fact about each fruit, you would use a hash: {"apple": "red", "banana": "yellow", "orange": "orange"}
Hashes are typically used when you want to pass some structured data around, lists when the data does not have any appreciable internal structure (apart from, perhaps, its order)
Due to the way hashes manage the key->value mapping, a hash typically does not retain the order of insertion. (so it's an unordered list of relations)
(Note: This is a pretty language-neutral answer, since these structures exist in all modern languages, and also in PHP.)
(also note that I've changed your topic title to be a little more helpful. you're welcome)
Hashes are typically used when you want to pass some structured data around, lists when the data does not have any appreciable internal structure (apart from, perhaps, its order)
Due to the way hashes manage the key->value mapping, a hash typically does not retain the order of insertion. (so it's an unordered list of relations)
(Note: This is a pretty language-neutral answer, since these structures exist in all modern languages, and also in PHP.)
(also note that I've changed your topic title to be a little more helpful. you're welcome)
Thanks! i appreciate your detailed answer. I got the concept now. Will practice more to perfect the concept.
Your answered helped a lot. Thanks
#4
Re: When to use a hash versus an array?
Posted 27 December 2016 - 05:03 AM
#5
Re: When to use a hash versus an array?
Posted 27 December 2016 - 11:56 AM
Page 1 of 1