Files
2012-02-21 01:15:00 -05:00

1 line
6.4 KiB
JSON

[{"user_id": 24848, "stars": [], "topic_id": 18210, "date_created": 1302335034.1666441, "message": "I have some idea of what Hashes and Array are good for, but it would be great if the community could share their thinking on what structure is better to use, when and why. Examples would be great too.", "group_id": 447, "id": 600903}, {"user_id": 408, "stars": [], "topic_id": 18210, "date_created": 1302335755.1167049, "message": "to be honest with you, they are really different. do you have any example that you can use either hash or array?", "group_id": 447, "id": 600983}, {"user_id": 26065, "stars": [{"date_created": 1302336190.6268201, "user_id": 24848}, {"date_created": 1305046970.6127369, "user_id": 9233}], "topic_id": 18210, "date_created": 1302336139.004555, "message": "Use an array when you index it's content by an integer and the index is without gaps (0..10, but not 0,4,7), use hashes for everything else, especially when the elements within a hash have different meaning (house: { front_door => ..., back_door => ...}", "group_id": 447, "id": 601048}, {"user_id": 4221, "stars": [], "topic_id": 18210, "date_created": 1302353354.579423, "message": "where with a hash, you know where it's at, so you just need to go there, get it and be done", "group_id": 447, "id": 601987}, {"user_id": 4221, "stars": [{"date_created": 1302534487.800303, "user_id": 22924}, {"date_created": 1303350126.1570499, "user_id": 16087}, {"date_created": 1304278676.96242, "user_id": 24848}], "topic_id": 18210, "date_created": 1302353239.5943739, "message": "I think it's best to understand what qualities Hashes and Arrays have. For instance, you can do quite a bit with both, and Ruby even gives you some flexibility to pretend a Hash is array-like (with the .each and .each_key ), but they are fundamentally different objects.\n\nArrays are list-like in that they can portray order, sequence, and indexing based on place in line.\n\nHashes are dictionary-like, in that you can get at an item directly by knowing what you're looking for. The difference is the ability to look up based on knowing the key for That Specific Thing, rather than stumbling around your house finding it.", "group_id": 447, "id": 601969}, {"user_id": 4221, "stars": [], "topic_id": 18210, "date_created": 1302353304.3607619, "message": "I mean, the difference is more like knowing exactly where you put your keys (\"I put the keys right next to my computer\") versus knowing kind-of where you put your keys (\"I put them in my office\").", "group_id": 447, "id": 601979}, {"user_id": 4221, "stars": [], "topic_id": 18210, "date_created": 1302353341.914269, "message": "with an array, you'd traverse the array of options hoping to find the element that is yoru key (\"is it by my desk? how bout on that shelf of the bookcase?\").", "group_id": 447, "id": 601984}, {"user_id": 4221, "stars": [], "topic_id": 18210, "date_created": 1302353509.0915279, "message": "So really, knowing when to use a Hash is more about knowing the domain of the problem. If you will know what you're looking for, such as people logging on to your site with their login ID, you could use a hash. it's straightforward, you don't have to traverse the whole array hoping to find it, and it's fast. If, however, you're looking for delinquent accounts, you might have to traverse a list of accounts and filter based on some quality of each of them.", "group_id": 447, "id": 602014}, {"user_id": 24848, "stars": [], "topic_id": 18210, "date_created": 1304278859.4722891, "message": "@klauern Thank you! I got \"in trouble\" with some friendly developers, as they were claiming just by asking this question I didn't understand the very basics of the 'hash vs. array'. However, the objective of the question was to actually get people to post examples of many different ways they've used hashes/array. This way whenever I (or somebody else) hits an unfamiliar but similar issue, we'd know better which approach had been tried, and which would yield better results. Just learning by crowd-sourcing! :)", "group_id": 447, "id": 877478}, {"user_id": 4221, "stars": [], "topic_id": 18210, "date_created": 1304364807.657079, "message": "I'm glad I could help. Too bad about the poor attitudes from other developers, though. Keep on keepin' on, as they say.", "group_id": 447, "id": 888127}, {"user_id": 31826, "stars": [], "topic_id": 18210, "date_created": 1304449757.555649, "message": "They are pretty fundamentally different - an array is usually for storing a collection of similar things. So you have 10 user objects that you want to deal with, it makes sense to store them in an array. A hash is useful for storing various things that aren't similar, but all belong to the same parent. So for an individual user, you might have hash of all of its attributes [:name => \"Tom\", :location => \"London\"] etc", "group_id": 447, "id": 901951}, {"user_id": 31826, "stars": [], "topic_id": 18210, "date_created": 1304449811.108098, "message": "Remember also that you can store hashes within arrays, and vice versa. So you could have an array of 10 users, where each element in the array was a hash of each user's attributes.", "group_id": 447, "id": 901971}, {"user_id": 31125, "stars": [], "topic_id": 18210, "date_created": 1304522267.910116, "message": "an array has an order, a hash does not", "group_id": 447, "id": 913952}, {"user_id": 28525, "stars": [], "topic_id": 18210, "date_created": 1304999108.473336, "message": "@sucrenoir, that's only true for Ruby 1.8.x. In Ruby 1.9.x, hash keeps its insertion order.", "group_id": 447, "id": 983739}, {"user_id": 31125, "stars": [], "topic_id": 18210, "date_created": 1305028366.5106499, "message": "@batasrki I didn't know that :) that's good !", "group_id": 447, "id": 988275}, {"user_id": 4221, "stars": [], "topic_id": 18210, "date_created": 1305111550.472611, "message": "For an array, I would just do thing[2], but I know Hash uses the [] as a key reference, so I would only be looking for a key that is 2, not the 3rd element.", "group_id": 447, "id": 1005803}, {"user_id": 4221, "stars": [], "topic_id": 18210, "date_created": 1305111500.02162, "message": "@batasrki So how does insertion order allow you to reference a specific entry? I want the third element of my hash. What's the syntax to retrieve that?", "group_id": 447, "id": 1005792}, {"user_id": 20089, "stars": [], "topic_id": 18210, "date_created": 1305165070.6359029, "message": "@klauern thing.values[2] would do it for ruby 1.9.x", "group_id": 447, "id": 1017268}]