Class: HashWithIndifferentAccess

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

Instance Method Summary collapse

Instance Method Details

#&(keys) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/hobosupport/hash.rb', line 75

def &(keys)
  res = HashWithIndifferentAccess.new
  keys.each do |k|
    k = k.to_s if k.is_a?(Symbol)
    res[k] = self[k] if has_key?(k)
  end
  res    
end

#-(keys) ⇒ Object



68
69
70
71
72
73
# File 'lib/hobosupport/hash.rb', line 68

def -(keys)
  res = HashWithIndifferentAccess.new
  keys = keys.map {|k| k.is_a?(Symbol) ? k.to_s : k }
  each_pair { |k, v| res[k] = v unless k.in?(keys) }
  res
end

#partition_hash(keys = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hobosupport/hash.rb', line 84

def partition_hash(keys=nil)
  keys = keys._?.map {|k| k.is_a?(Symbol) ? k.to_s : k }
  yes = HashWithIndifferentAccess.new
  no = HashWithIndifferentAccess.new
  each do |k,v|
    if block_given? ? yield(k,v) : keys.include?(k)
      yes[k] = v
    else
      no[k] = v
    end
  end
  [yes, no]
end