Class Hash
In: lib/facet/hash/collate.rb
lib/facet/hash/collect.rb
lib/facet/hash/<<.rb
lib/facet/hash/[].rb
lib/facet/hash/swap!.rb
lib/facet/hash/each.rb
lib/facet/hash/to_ostruct.rb
lib/facet/hash/zipnew.rb
lib/facet/hash/inverse.rb
lib/facet/hash/swapkey!.rb
lib/facet/hash/keys_to_sym.rb
lib/facet/hash/at_rand.rb
lib/facet/hash/to_h.rb
lib/facet/hash/alias!.rb
lib/facet/hash/slice.rb
lib/facet/hash/traverse.rb
lib/facet/hash/shuffle.rb
lib/facet/hash/has_keys?.rb
lib/facet/hash/keys_to_s.rb
lib/facet/hash/weave.rb
lib/facet/hash/update_each.rb
Parent: Object

Methods

External Aliases

graph -> collect
  Hash#collect will return a hash rather then an array. It is simple an alias to Enumerable#graph.
graph! -> collect!
update -> <<
  Alias shorthand for update.
[] -> at
values_at -> slice
  Alias slice to values_at.
  require "facet/hash/slice"

  h = { :a => 1, :b => 2, :c => 3 }
  h.slice(:a)      #=> [1]
  h.slice(:a, :b)  #=> [1,2]

Public Class methods

Creates a new hash from two arrays —a keys array and a values array.

  require 'facet/hash/zipnew'

  Hah.zipnew(["a","b","c"], [1,2,3])
    #=> { "a"=>1, "b"=>2, "c"=>3 }

Public Instance methods

Adds slicing to Hash#[]. If more than one key arguments is given to Hash#[], the return value will be an array of the corresponding values.

  require "facet/hash/[]"

  h = { :a => 1, :b => 2, :c => 3 }
  h[:a]            #=> 1
  h[:a, :b]        #=> [1,2]

Add slicing to element assignment operator.

  require "facet/hash/[]"

  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.

  require 'facet/hash/alias!'

  foo = { :name=>'Gavin', 'wife'=>:Lisa }
  foo.alias!('name',:name)     => { :name=>'Gavin', 'name'=>'Gavin', 'wife'=>:Lisa }
  foo.alias!('spouse','wife')  => { :name=>'Gavin', 'name'=>'Gavin', 'wife'=>:Lisa, 'spouse'=>:Lisa }
  foo.alias!('bar','foo')      => { :name=>'Gavin', 'name'=>'Gavin', 'wife'=>:Lisa, 'spouse'=>:Lisa }

Note that if the oldkey is reassigned, the reference will no longer exist, and the newkey will remain as it was.

require ‘facet/hash/has_keys?‘

h = { :a => 1, :b => 2 } h.assert_has_keys( :a ) #=> true h.assert_has_keys( :c ) #=> ArgumentError

require ‘facet/hash/has_keys?‘

h = { :a => 1, :b => 2 } h.assert_only_keys( :a, :b ) #=> true h.assert_only_keys( :a ) #=> ArgumentError

at_rand()

Alias for rand_value

at_rand!()

Alias for rand_value!

Returns a new hash built by iterating through each key,value pair and updating the new hash.

  require 'facet/hash/collate'

In place version of collate.

convert_keys( from_class=nil )

Alias for keys_to_s

convert_keys!( from_class=nil )

Alias for keys_to_s!

A "smarter" hasheach which iterates through each value if only one block parameter is given.

  require 'facet/hash/each'

  {:a=>"a", 2=>"b", "x"=>"c"}.each{ |v| puts v }

produces

  a
  b
  c

WARNING! Use with caution. Methods from other libraries may depend on the old behavior, expecting a two element array to be passed into a single block argument.

require ‘facet/hash/has_keys?‘

h = { :a => 1, :b => 2 } h.has_keys?( :a ) #=> true h.has_keys?( :c ) #=> false

require ‘facet/hash/has_keys?‘

h = { :a => 1, :b => 2 } h.has_only_keys?( :a, :b ) #=> true h.has_only_keys?( :a ) #=> false

Create a "true" inverse hash by storing mutliple values in Arrays.

  require 'facet/hash/inverse'

  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 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.

  require 'facet/hash/keys_to_string'

  foo = { :name=>'Gavin', :wife=>:Lisa }
  foo.to_s_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.

  require 'facet/hash/keys_to_string'

  foo = { :name=>'Gavin', :wife=>:Lisa }
  foo.to_s_keys!    #=>  { "name"=>"Gavin", "wife"=>:Lisa }
  foo.inspect       #=>  { "name"=>"Gavin", "wife"=>:Lisa }

Converts all keys in the Hash to be Symbol values, returning a new Hash. With a from_class parameter, limits conversion to only a certain class of keys. It defaults to String —use nil to convert any key class.

  require 'facet/hash/keys_to_symbol'

  foo = { :name=>'Gavin', 'wife'=>:Lisa }
  foo.keys_to_sym    #=>  { :name=>"Gavin", :wife=>:Lisa }
  foo.inspect        #=>  { "name" =>"Gavin", "wife"=>:Lisa }

Synonym for Hash#keys_to_symbol, 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 String —use nil to convert any key class.

  require 'facet/hash/keys_to_symbol'

  foo = { 'name'=>'Gavin', 'wife'=>:Lisa }
  foo.keys_to_sym!    #=>  { :name=>"Gavin", :wife=>:Lisa }
  foo.inspect         #=>  { :name=>"Gavin", :wife=>:Lisa }
pick()

Alias for at_rand!

pick_key()

Alias for rand_key!

pick_pair()

Alias for rand_pair!

Returns a random key.

  require 'facet/hash/at_rand'

  {:one => 1, :two => 2, :three => 3}.pick_key  #=> :three

Delete a random key-value pair, returning the key.

  require 'facet/hash/at_rand'

  a = {:one => 1, :two => 2, :three => 3}
  a.pick_key!  #=> :two
  a            #=> {:one => 1, :three => 3}

Returns a random key-value pair.

  require 'facet/hash/at_rand'

  {:one => 1, :two => 2, :three => 3}.pick  #=> [:one, 1]

Deletes a random key-value pair and returns that pair.

  require 'facet/hash/at_rand'

  a = {:one => 1, :two => 2, :three => 3}
  a.rand_pair!  #=> [:two, 2]
  a             #=> {:one => 1, :three => 3}

Returns a random key-value pair.

  require 'facet/hash/at_rand'

  {:one => 1, :two => 2, :three => 3}.at_rand  #=> [:one, 1]

Deletes a random key-value pair and returns the value.

  require 'facet/hash/at_rand'

  a = {:one => 1, :two => 2, :three => 3}
  a.at_rand!  #=> 2
  a           #=> {:one => 1, :three => 3}

Same as update_each, but deletes the key element first.

Returns a copy of the hash with values arranged in new random order.

  require 'facet/hash/shuffle'

  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.

  require 'facet/hash/shuffle'

  h = {:a => 1, :b => 2, :c => 3}
  h.shuffle_hash!
  h  #=> {:b=>2, :c=>1, :a=>3}

Return the key-value pairs with keys and values shuffled independedly.

  require 'facet/hash/shuffle'

  {:a=>1, :b=>2, :c=>3}.shuffle_hash_pairs

produces

  [[:a, 3], [:b, 1], [:c, 2]]

Swap the values of a pair of keys.

  require 'facet/hash/swap!'

  {:a=>1,:b=>2}.swap!  #=> {:a=>2,:b=>1}

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.

  require 'facet/hash/swapkey!'

  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 }
symbolize_keys( from_class=String )

Alias for keys_to_sym

symbolize_keys!( from_class=String )

Alias for keys_to_sym!

Return a rehashing of self.

  require 'facet/hash/to_h'

  {"a"=>1,"b"=>2}.to_h  #=> {"b"=>2,"a"=>1}

Turns a hash into a generic object using an OpenStruct.

  require 'facet/hash/to_ostruct'

  o = { 'a' => 1 }.to_ostruct
  o.a  #=> 1

Like to_ostruct but recusively objectifies all hash elements as well.

  require 'facet/hash/to_ostruct'

  o = { 'a' => { 'b' => 1 } }.to_ostruct_recurse
  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]+.

  require 'facet/hash/traverse'

  h = { "A"=>"A", "B"=>"B" }
  h.traverse { |k,v| [k.downcase, v] }
  h  #=> { "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.

  require 'facet/hash/traverse'

  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.

  require 'facet/hash/update_each'

  # example to do

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:

  require 'facet/hash/weave'

  # to do

[Validate]