4 Replies - 1096 Views - Last Post: 27 December 2016 - 11:56 AM Rate Topic: -----

#1 lawkimsun  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 5
  • Joined: 23-December 16

When to use a hash versus an array?

Posted 23 December 2016 - 06:42 PM

/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
Is This A Good Question/Topic? 0
  • +

Replies To: When to use a hash versus an array?

#2 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10682
  • View blog
  • Posts: 18,296
  • Joined: 19-March 11

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)
Was This Post Helpful? 1
  • +
  • -

#3 lawkimsun  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 5
  • Joined: 23-December 16

Re: When to use a hash versus an array?

Posted 27 December 2016 - 04:41 AM

View Postjon.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)


Thanks! i appreciate your detailed answer. I got the concept now. Will practice more to perfect the concept.

Your answered helped a lot. Thanks
Was This Post Helpful? 2
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,983
  • Joined: 21-June 11

Re: When to use a hash versus an array?

Posted 27 December 2016 - 05:03 AM

View Postjon.kiparsky, on 24 December 2016 - 05:58 AM, said:

Due to the way hashes manage the key->value mapping, a hash typically does not retain the order of insertion.


They do keep their insertion order since Ruby 1.9, I believe.
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10682
  • View blog
  • Posts: 18,296
  • Joined: 19-March 11

Re: When to use a hash versus an array?

Posted 27 December 2016 - 11:56 AM

View Postlawkimsun, on 27 December 2016 - 06:41 AM, said:

Your answered helped a lot. Thanks


Glad I could help!

Quote

Will practice more to perfect the concept.


+1. This is the right idea. Have fun!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1