[] | -> | / |
Use division as a fetch notation. | ||
each_with_key | -> | each_with_index |
More appropriate naming and polymorphism with Array. Use each_with_counter for old functionality. | ||
update | -> | << |
Alias shorthand for update. | ||
values_at | -> | slice |
Alias slice to values_at.
h = { :a => 1, :b => 2, :c => 3 } h.slice(:a) #=> [1] h.slice(:a, :b) #=> [1,2] |
||
delete | -> | delete_at |
fallback | [RW] |
Hash which auto initializes it‘s children.
ah = Hash.auto ah['section one']['param one'] = 4 ah['section one']['param two'] = 5 ah['section one']['param three'] = 2 ah['section one']['param four'] = 3 p ah # {"section one"=>{"param one"=>4, "param four"=>3, "param three"=>2, "param two"=>5}} p ah['section one'].keys # ["param one", "param four", "param three", "param two"]
Creates a new hash from two arrays —a keys array and a values array.
Hash.zipnew(["a","b","c"], [1,2,3]) #=> { "a"=>1, "b"=>2, "c"=>3 }
Hash intersection. Two hashes intersect when their pairs are equal.
{:a=>1,:b=>2} & {:a=>1,:c=>3} #=> {:a=>1}
A hash can also be intersected with an array to insterstec keys only.
{:a=>1,:b=>2} & [:a,:c] #=> {:a=>1}
The later form is simalar to pairs_at. The differ only in that pairs_at will return a nil value for a key not in the hash, but #& will not.
Like merge operator ’+’ but merges in reverse order.
h1 = { :a=>1 } h2 = { :a=>2, :b=>3 } h1 + h2 #=> { :a=>2, :b=>3 } h1 * h2 #=> { :a=>1, :b=>3 }
Operator for remove hash paris. If another hash is given the pairs are only removed if both key and value are equal. If an array is given then mathcing keys are removed.
Add slicing to element assignment operator.
h = {:a=>1, :b=>2, :c=>3} h[:a] = 9 #=> 9 h #=> {:a=>9, :b=>2, :c=>3} h[:a, :c] = [10,11] #=> [10,11] h #=> {:a=>10, :b=>2, :c=>11}
Modifies the receiving Hash so that the value previously referred to by oldkey is also referenced by newkey; oldkey is retained in the Hash. If oldkey does not exist as a key in the Hash, no change is effected.
Returns a reference to the Hash.
foo = { :name=>'Gavin', 'wife'=>:Lisa } foo.alias!('name',:name) => { :name=>'Gavin', 'name'=>'Gavin', 'wife'=>:Lisa } foo = { :name=>'Gavin', 'wife'=>:Lisa } foo.alias!('spouse','wife') => { :name=>'Gavin', 'wife'=>:Lisa, 'spouse'=>:Lisa } foo = { :name=>'Gavin', 'wife'=>:Lisa } foo.alias!('bar','foo') => { :name=>'Gavin', 'wife'=>:Lisa }
Note that if the oldkey is reassigned, the reference will no longer exist, and the newkey will remain as it was.
Returns true is hash has the given keys, otherwise throws an ArgumentError.
h = { :a => 1, :b => 2 } h.assert_has_keys( :a ) #=> true h.assert_has_keys( :c ) #=> ArgumentError
Returns true is hash has only then given keys, otherwise throws an ArgumentError.
h = { :a => 1, :b => 2 } h.assert_has_only_keys( :a, :b ) #=> true h.assert_has_only_keys( :a ) #=> ArgumentError
Minor modification to Ruby‘s Hash#delete method allowing it to take multiple keys.
hsh = { :a => 1, :b => 2 } hsh.delete_values(1) hsh #=> { :b => 2 }
Minor modification to Ruby‘s Hash#delete method allowing it to take multiple keys.
This works niely with hahs#[] and Hash#[]= facets.
hsh[:a, :b, :c] = 1, 2, 3 a, b, c = hsh.delete_values_at(:a, :b, :c) [a, b, c] #=> [1, 2, 3] hsh #=> {}
Returns true or false whether the hash contains the given keys.
h = { :a => 1, :b => 2 } h.has_keys?( :a ) #=> true h.has_keys?( :c ) #=> false
Returns true if the hash contains only the given keys, otherwise false.
h = { :a => 1, :b => 2 } h.has_only_keys?( :a, :b ) #=> true h.has_only_keys?( :a ) #=> false
Define a fallback object for fetch and #[].
f = Hash[:b=>2] h = Hash[:a=>1].having_fallback(f) h[:b] => 2
Create a "true" inverse hash by storing mutliple values in Arrays.
h = {"a"=>3, "b"=>3, "c"=>3, "d"=>2, "e"=>9, "f"=>3, "g"=>9} h.invert #=> {2=>"d", 3=>"f", 9=>"g"} h.inverse #=> {2=>"d", 3=>["f", "c", "b", "a"], 9=>["g", "e"]} h.inverse.inverse #=> {"a"=>3, "b"=>3, "c"=>3, "d"=>2, "e"=>9, "f"=>3, "g"=>9} h.inverse.inverse == h #=> true
Converts all keys in the Hash accroding to the given block. If the block return nil for given key, then that key will be left intact.
foo = { :name=>'Gavin', :wife=>:Lisa } foo.normalize_keys{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa } foo.inspect #=> { :name =>"Gavin", :wife=>:Lisa }
Synonym for Hash#normalize_keys, but modifies the receiver in place (and returns it).
foo = { :name=>'Gavin', :wife=>:Lisa } foo.normalize_keys!{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa } foo.inspect #=> { "name"=>"Gavin", "wife"=>:Lisa }
Return a new hash with the include entires.
{:a=>1,:b=>2}.pairs_at(:a,:c) #=> {:a=>1, :c=>nil}
The later form is equivalent to pairs_at.
Delete a random key-value pair, returning the key.
a = {:one => 1, :two => 2, :three => 3} a.pick_key! #=> :two a #=> {:one => 1, :three => 3}
Deletes a random key-value pair and returns that pair.
a = {:one => 1, :two => 2, :three => 3} a.rand_pair! #=> [:two, 2] a #=> {:one => 1, :three => 3}
Returns a random hash value.
{:one => 1, :two => 2, :three => 3}.rand_value #=> 2 {:one => 1, :two => 2, :three => 3}.rand_value #=> 1
Deletes a random key-value pair and returns the value.
a = {:one => 1, :two => 2, :three => 3} a.at_rand! #=> 2 a #=> {:one => 1, :three => 3}
Converts all keys in the Hash accroding to the given block. If the block return nil for given key, then that key will be left intact.
foo = { :name=>'Gavin', :wife=>:Lisa } foo.rekey{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa } foo.inspect #=> { :name =>"Gavin", :wife=>:Lisa }
Synonym for Hash#rekey, but modifies the receiver in place (and returns it).
foo = { :name=>'Gavin', :wife=>:Lisa } foo.rekey!{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa } foo.inspect #=> { "name"=>"Gavin", "wife"=>:Lisa }
Allows for reverse merging where its the keys in the calling hash that wins over those in the other_hash. This is particularly useful for initializing an incoming option hash with default values:
def setup(options = {}) options.reverse_merge! :size => 25, :velocity => 10 end
The default :size and :velocity is only set if the options passed in doesn‘t already have those keys set.
Returns a copy of the hash with values arranged in new random order.
h = {:a=>1, :b=>2, :c=>3} h.shuffle_hash #=> {:b=>2, :c=>1, :a>3}
Destructive shuffle_hash. Arrange the values in a new random order.
h = {:a => 1, :b => 2, :c => 3} h.shuffle_hash! h #=> {:b=>2, :c=>1, :a=>3}
Synonym for Hash#stringify_keys, but modifies the receiver in place and returns it. With a filter parameter, limits conversion to only a certain selection of keys.
foo = { :name=>'Gavin', :wife=>:Lisa } foo.stringify_keys! #=> { "name"=>"Gavin", "wife"=>:Lisa } foo.inspect #=> { "name"=>"Gavin", "wife"=>:Lisa }
Modifies the receiving Hash so that the value previously referred to by oldkey is referenced by newkey; oldkey is removed from the Hash. If oldkey does not exist as a key in the Hash, no change is effected.
Returns a reference to the Hash.
foo = { :a=>1, :b=>2 } foo.swapkey!('a',:a) #=> { 'a'=>1, :b=>2 } foo.swapkey!('b',:b) #=> { 'a'=>1, 'b'=>2 } foo.swapkey!('foo','bar') #=> { 'a'=>1, 'b'=>2 }
Synonym for Hash#symbolize_keys, but modifies the receiver in place and returns it. With a filter parameter, limits conversion to only a certain selection of keys.
foo = { 'name'=>'Gavin', 'wife'=>:Lisa } foo.symbolize_keys! #=> { :name=>"Gavin", :wife=>:Lisa } foo.inspect #=> { :name=>"Gavin", :wife=>:Lisa }
Returns a JSON string containing a JSON object, that is unparsed from this Hash instance. state is a JSON::State object, that can also be used to configure the produced JSON string output further. depth is used to find out nesting depth, to indent accordingly.
Turns a hash into a generic object using an OpenStruct.
o = { 'a' => 1 }.to_ostruct o.a #=> 1
Like to_ostruct but recusively objectifies all hash elements as well.
o = { 'a' => { 'b' => 1 } }.to_ostruct_recurse o.a.b #=> 1
The exclude parameter is used internally to prevent infinite recursion and is not intended to be utilized by the end-user. But for more advance use, if there is a particular subhash you would like to prevent from being converted to an OpoenStruct then include it in the exclude hash referencing itself. Eg.
h = { 'a' => { 'b' => 1 } } o = h.to_ostruct_recurse( { h['a'] => h['a'] } ) o.a['b'] #=> 1
Returns a new hash created by traversing the hash and its subhashes, executing the given block on the key and value. The block should return a 2-element array of the form +[key, value]+.
h = { "A"=>"A", "B"=>"B" } g = h.traverse { |k,v| [k.downcase, v] } g #=> { "a"=>"A", "b"=>"B" }
In place version of traverse, which traverses the hash and its subhashes, executing the given block on the key and value.
h = { "A"=>"A", "B"=>"B" } h.traverse! { |k,v| [k.downcase, v] } h #=> { "a"=>"A", "b"=>"B" }
Iterates through each pair and updates a the hash in place. This is formally equivalent to collate! But does not use collate to accomplish the task. Hence update_each is probably a bit faster.
# TODO
Converts all keys in the Hash to be String values, returning a new Hash. With a from_class parameter, limits conversion to only a certain class of keys. It defaults to nil which convert any key class.
foo = { :name=>'Gavin', :wife=>:Lisa } foo.variablize_keys #=> { "@name"=>"Gavin", "@wife"=>:Lisa } foo.inspect #=> { :name =>"Gavin", :wife=>:Lisa }
Synonym for Hash#keys_to_string, but modifies the receiver in place (and returns it). With a from_class parameter, limits conversion to only a certain class of keys. It defaults to nil which convert any key class.
foo = { :name=>'Gavin', :wife=>:Lisa } foo.variablize_keys! #=> { "@name"=>"Gavin", "@wife"=>:Lisa } foo.inspect #=> { "@name"=>"Gavin", "@wife"=>:Lisa }
Weaves two hashes producing a new hash. The two hashes need to be compatible according to the following rules for each node:
<tt> hash, hash => hash (recursive +) hash, array => error hash, value => error array, hash => error array, array => array + array array, value => array << value value, hash => error value, array => array.unshift(valueB) valueA, valueB => valueB </tt>
Example:
# to do
Define a fallback object for fetch and #[].
f = Hash[:b=>2] h = Hash[:a=>1].having_aquisition(f) h[:b] => 2