Class: Hash
- 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
-
#except(*keys) ⇒ Object
Filter keys out of a Hash.
-
#only(*keys) ⇒ Object
Returns a hash with only the pairs identified by
keys. -
#rand_key ⇒ Object
returns a key chosen at from this array.
-
#rand_pair ⇒ Object
returns a key chosen at from this array.
-
#rand_value ⇒ Object
returns a key chosen at from this array.
-
#with(overrides = {}) ⇒ Object
Merge keys into a Hash.
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:
-
Neil Rahilly
-
Peepcode episode 11. Geoffrey Grosenbach alludes to this being a common idiom among rSpec coders.
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:
-
Neil Rahilly
-
Peepcode episode 11. Geoffrey Grosenbach alludes to this being a common idiom among rSpec coders.
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_key ⇒ Object
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_pair ⇒ Object
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_value ⇒ Object
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:
-
Neil Rahilly
-
Peepcode episode 11. Geoffrey Grosenbach alludes to this being a common idiom among rSpec coders.
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 |