Re: [Rails] Custom order of Hash

2014-02-24 Thread Artem Kalinchuk
Honestly, I've needed it many times and was surprised that it wasn't implemented yet. Of course, there are a few ways you can do this (probably more efficient way than what I suggested) but we also need to keep in mind that we should keep our code as DRY as possible. On Monday, February 24, 201

Re: [Rails] Custom order of Hash

2014-02-24 Thread Artem Kalinchuk
There is a reason why we have ordered hashes... On Monday, February 24, 2014 11:53:04 AM UTC-5, Scott Ribe wrote: > > On Feb 24, 2014, at 8:28 AM, Artem Kalinchuk > > wrote: > > > Any thoughts? > > Hashes are by definition unordered. If you think you need an ordered hash, > then you probably d

Re: [Rails] Custom order of Hash

2014-02-24 Thread Gautam Rege
> Hashes are by definition unordered. If you think you need an ordered hash, > then you probably don't understand hashes... Ruby 1.9 onwards saves the insertion order of hashes. In fact in Ruby 2.1 - a hash of under 6 elements is stored as an array ;) 2.1.0 :001 > hsh = {'a' => 1, 'b' => 2, 'c

Re: [Rails] Custom order of Hash

2014-02-24 Thread Scott Ribe
On Feb 24, 2014, at 8:28 AM, Artem Kalinchuk wrote: > Any thoughts? Hashes are by definition unordered. If you think you need an ordered hash, then you probably don't understand hashes... -- Scott Ribe scott_r...@elevated-dev.com http://www.elevated-dev.com/ (303) 722-0567 voice -- You r

Re: [Rails] Custom order of Hash

2014-02-24 Thread Gautam Rege
Having an OrderedHash that saves insertion order is useful enough and I personally don't see a reason for a custom ordered hash. Wouldn't it be less expensive to order only the keys in a separate array and access the hash rather than creating a new hash? My 2 cents. -- @gautamrege ~~

[Rails] Custom order of Hash

2014-02-24 Thread Artem Kalinchuk
Would a custom order of a hash be useful in Rails? If I have the following Hash: my_hash = { key1: 'value', key2: 'value', key3: 'value' } And I want to order it by the following keys: my_hash = my_hash.order(:key2, :key1, :key3) I would get a new hash with the following result: puts my_has