I have this as a hash :
我把这个当成散列:
>> params[:payments]
{"0"=>{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"}}
{"1"=>{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"}}
{"2"=>{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"}}
Is it possible to make a new hash and insert it into a specific place ? For example, take this line :
是否可以创建一个新的散列并将其插入到特定的位置?例如,这句话:
{"2"=>{":amount_paid"=>"1.00", ":date_paid"=>"1/1/2006"}}
So that the hash will appear as this now :
这样哈希就会变成这样:
{"0"=>{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"}}
{"1"=>{":amount_paid"=>"1.00", ":date_paid"=>"1/1/2006"}} # <-- Newly inserted line
{"2"=>{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"}}
{"3"=>{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"}}
3 个解决方案
#1
4
Try using an Array, as hashes are not inherently ordered. Ruby's Array.insert lets you specify where to insert your new objects (http://www.ruby-doc.org/core/classes/Array.html#M000230), and using your example above, we get:
尝试使用一个数组,因为散列不是天生有序的。Ruby的数组。插入允许您指定插入新对象的位置(http://www.ruby-doc.org/core/classes/Array.html#M000230),使用上面的示例,我们得到:
arr = [{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"},
{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"},
{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"}]
arr.insert(2, {"some hash"})
[{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"},
{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"},
{"some hash"},
{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"}]
You can then iterate over the indices and maintain your desired order while adding to it.
然后,您可以遍历索引,并在添加索引时保持所需的顺序。
#2
3
In Ruby 1.9+, Hashes are not ordered, instead, they maintain the order that entries are inserted:
在Ruby 1.9+中,散列不是有序的,而是保持插入条目的顺序:
From the docs:
从文档:
Hashes enumerate their values in the order that the corresponding keys were inserted.
散列按插入相应键的顺序枚举它们的值。
require 'pp'
hash = {
'a' => 1,
'c' => 3
}
hash['b'] = 2
pp hash
# >> {"a"=>1, "c"=>3, "b"=>2}
As an alternate to sorting the hash keys then iterating over the hash, the hash itself can be sorted, then regenerated if you want to walk it directly:
作为对哈希键进行排序然后遍历哈希的替代方法,哈希本身可以被排序,如果你想直接遍历它,可以重新生成:
ruby-1.9.2-p136 :010 > Hash[*hash.sort.flatten]
=> {"a"=>1, "b"=>2, "c"=>3}
Use it like:
使用它:
hash = Hash[*hash.sort.flatten]
It's probably faster with a big hash to sort the keys then walk them, but if you gotta have the hash in order that will do it.
用一个大哈希对键进行排序可能会更快,但如果你要让哈希按顺序排列的话,就可以了。
#3
1
If you had to do this with a Hash, and you didn't want to use the index of an array, you would create a new class, OrderedHash. In that class, you need to create an insert
and delete
method that enforces the following properties:
如果您必须使用散列,并且不想使用数组的索引,那么您将创建一个新类OrderedHash。在该类中,您需要创建一个插入和删除方法,该方法强制执行以下属性:
Insert:
插入:
- a new entry has a key that is in range (<= the largest current key).
- 新条目有一个位于范围内的键(<=当前最大的键)。
- If a new entry has a key that is already used, for each number from the current new key value to the largest value, increment the key value by one. Probably best to do this from the largest down to the smallest to avoid problems.
- 如果一个新条目有一个已经使用的键,对于从当前新键值到最大值的每个数字,将键值增加1。也许最好从最大的到最小的来避免问题。
- Insert new Entry
- 插入新条目
Delete: When an entry is deleted, decrement the key value for all keys > the deletion value.
删除:删除一个条目时,将所有键的键值减去删除值>。
#1
4
Try using an Array, as hashes are not inherently ordered. Ruby's Array.insert lets you specify where to insert your new objects (http://www.ruby-doc.org/core/classes/Array.html#M000230), and using your example above, we get:
尝试使用一个数组,因为散列不是天生有序的。Ruby的数组。插入允许您指定插入新对象的位置(http://www.ruby-doc.org/core/classes/Array.html#M000230),使用上面的示例,我们得到:
arr = [{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"},
{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"},
{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"}]
arr.insert(2, {"some hash"})
[{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"},
{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"},
{"some hash"},
{":amount_paid"=>"100.00", ":date_paid"=>"2/20/2009"}]
You can then iterate over the indices and maintain your desired order while adding to it.
然后,您可以遍历索引,并在添加索引时保持所需的顺序。
#2
3
In Ruby 1.9+, Hashes are not ordered, instead, they maintain the order that entries are inserted:
在Ruby 1.9+中,散列不是有序的,而是保持插入条目的顺序:
From the docs:
从文档:
Hashes enumerate their values in the order that the corresponding keys were inserted.
散列按插入相应键的顺序枚举它们的值。
require 'pp'
hash = {
'a' => 1,
'c' => 3
}
hash['b'] = 2
pp hash
# >> {"a"=>1, "c"=>3, "b"=>2}
As an alternate to sorting the hash keys then iterating over the hash, the hash itself can be sorted, then regenerated if you want to walk it directly:
作为对哈希键进行排序然后遍历哈希的替代方法,哈希本身可以被排序,如果你想直接遍历它,可以重新生成:
ruby-1.9.2-p136 :010 > Hash[*hash.sort.flatten]
=> {"a"=>1, "b"=>2, "c"=>3}
Use it like:
使用它:
hash = Hash[*hash.sort.flatten]
It's probably faster with a big hash to sort the keys then walk them, but if you gotta have the hash in order that will do it.
用一个大哈希对键进行排序可能会更快,但如果你要让哈希按顺序排列的话,就可以了。
#3
1
If you had to do this with a Hash, and you didn't want to use the index of an array, you would create a new class, OrderedHash. In that class, you need to create an insert
and delete
method that enforces the following properties:
如果您必须使用散列,并且不想使用数组的索引,那么您将创建一个新类OrderedHash。在该类中,您需要创建一个插入和删除方法,该方法强制执行以下属性:
Insert:
插入:
- a new entry has a key that is in range (<= the largest current key).
- 新条目有一个位于范围内的键(<=当前最大的键)。
- If a new entry has a key that is already used, for each number from the current new key value to the largest value, increment the key value by one. Probably best to do this from the largest down to the smallest to avoid problems.
- 如果一个新条目有一个已经使用的键,对于从当前新键值到最大值的每个数字,将键值增加1。也许最好从最大的到最小的来避免问题。
- Insert new Entry
- 插入新条目
Delete: When an entry is deleted, decrement the key value for all keys > the deletion value.
删除:删除一个条目时,将所有键的键值减去删除值>。