Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/viral_seq/hash.rb

Overview

additional methods for Class::Hash required for ViralSeq

Instance Method Summary collapse

Instance Method Details

#difference(other_hash) ⇒ Hash

subtract one hash (h2) from the other (h1) if the keys are identical

Examples:

substract h2 from h1 if the keys match

h1 = {"Cat" => 100, "Dog" => 5, "Bird" => 2, "Snake" => 10}
h2 = {"Cat" => 100, "Dog" => 5, "Bison" => 30}
h1.difference(h2)
=> {"Bird" => 2, "Snake" => 10}

Parameters:

  • other_hash (Hash)

    the hash that needs to substracted from the hash before the method

Returns:

  • (Hash)

    hash after substraction



14
15
16
17
18
# File 'lib/viral_seq/hash.rb', line 14

def difference(other_hash)
  reject do |k,_v|
    other_hash.has_key? k
  end
end

#uniq_hashHash

return a new hash with the unique values of input hash as keys, and the keys of the unique values of input hash in an array as values of the new hash

Examples:

hash = {1=>"A", 2=>"A", 3=>"C", 4=>"C", 5=>"T"}
hash.uniq_hash
=> {"A"=>[1, 2], "C"=>[3, 4], "T"=>[5]}

Returns:

  • (Hash)

    a new hash of :uniq_value_of_orginial_hash => :array_of_keys



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/viral_seq/hash.rb', line 28

def uniq_hash
  uniq_values = self.values.uniq
  out_hash = {}
  uniq_values.each do |uniq_va|
    self.each do |k,v|
      if v == uniq_va
        if out_hash[uniq_va]
          out_hash[uniq_va] << k
        else
          out_hash[uniq_va] = []
          out_hash[uniq_va] << k
        end
      end
    end
  end
  return out_hash
end