Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/can_has_support/hash_core_ext.rb

Instance Method Summary collapse

Instance Method Details

#except(*keys) ⇒ Object

These are stealed from Geoff of Peepcode fame

Filter keys out of a Hash.

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


8
9
10
# File 'lib/can_has_support/hash_core_ext.rb', line 8

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.

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


24
25
26
# File 'lib/can_has_support/hash_core_ext.rb', line 24

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

#with(overrides = {}) ⇒ Object

Override some keys.

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


16
17
18
# File 'lib/can_has_support/hash_core_ext.rb', line 16

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