Class: Hash

Inherits:
Object show all
Defined in:
lib/kitchensink/patches/hash.rb,
lib/kitchensink/patches/hash.rb,
lib/kitchensink/patches/hash.rb,
lib/kitchensink/patches/hash.rb,
lib/kitchensink/patches/hash.rb,
lib/kitchensink/patches/hash.rb

Instance Method Summary collapse

Instance Method Details

#except(*keys) ⇒ Object

Filter keys out of a Hash.

>> {:a=>1, :b=>2, :c=>3}.except(:a)
=> {:b=>2, :c=>3}

Source:



13
14
15
# File 'lib/kitchensink/patches/hash.rb', line 13

def except(*keys)
  self.reject { |k,v| keys.include?(k || k.to_sym) }
end

#only(*keys) ⇒ Object

Returns a hash with only the pairs identified by keys

Source:



28
29
30
# File 'lib/kitchensink/patches/hash.rb', line 28

def only(*keys)
  self.reject { |k,v| !keys.include?(k || k.to_sym) }
end

#rand_keyObject

returns a key chosen at from this array



37
38
39
# File 'lib/kitchensink/patches/hash.rb', line 37

def rand_key
  keys[rand(keys.size)]
end

#rand_pairObject

returns a key chosen at from this array



46
47
48
49
# File 'lib/kitchensink/patches/hash.rb', line 46

def rand_pair
  k = rand_key
  [k, self[k]]
end

#rand_valueObject

returns a key chosen at from this array



56
57
58
# File 'lib/kitchensink/patches/hash.rb', line 56

def rand_value
  self[rand_key]
end

#with(overrides = {}) ⇒ Object

Merge keys into a Hash.

>> {:a=>1, :b=>2, :c=>3}.with({:a=>4,:b=>5})
=> {:a=>4, :b=>5, :c=>3}

Source:

Known Issue: This method conflicts with utility_belt’s with statement.



77
78
79
# File 'lib/kitchensink/patches/hash.rb', line 77

def with(overrides = {})
  self.merge overrides
end