Class: Hash

Inherits:
Object show all
Defined in:
lib/babushka/core_patches/hash.rb

Instance Method Summary collapse

Instance Method Details

#defaults(other) ⇒ Object

Return a new hash consisting of other reverse-merged into this hash; that is, equal to other.merge(self).



38
39
40
# File 'lib/babushka/core_patches/hash.rb', line 38

def defaults other
  dup.defaults! other
end

#defaults!(other) ⇒ Object

Reverse-merge other into this hash in-place; that is, merge all key-value pairs in other whose keys are not already present in self.



32
33
34
# File 'lib/babushka/core_patches/hash.rb', line 32

def defaults! other
  replace other.merge(self)
end

#discard(*keys) ⇒ Object

Return a new hash filtered to exclude any key-value pairs whose keys appear in keys.



4
5
6
# File 'lib/babushka/core_patches/hash.rb', line 4

def discard *keys
  dup.discard!(*keys)
end

#discard!(*keys) ⇒ Object

Filter this hash in-place so any key-value pairs whose keys appear in keys are removed.



10
11
12
13
# File 'lib/babushka/core_patches/hash.rb', line 10

def discard! *keys
  keys.each {|k| delete k }
  self
end

#map_values(&block) ⇒ Object

Return a new hash with the same keys as this hash, but with values generated by yielding each key-value pair to block.



44
45
46
# File 'lib/babushka/core_patches/hash.rb', line 44

def map_values &block
  dup.map_values!(&block)
end

#map_values!(&block) ⇒ Object

Update this hash in-place by replacing each value with the result of yielding the corresponding key-value pair to the block.



50
51
52
53
54
55
# File 'lib/babushka/core_patches/hash.rb', line 50

def map_values! &block
  keys.each {|k|
    self[k] = yield k, self[k]
  }
  self
end

#reject_r(&block) ⇒ Object

Return a new hash recursively filtered to remove all key-value pairs for which the block returned true. That is, like Hash#reject, except sub-hashes are also filtered.



71
72
73
# File 'lib/babushka/core_patches/hash.rb', line 71

def reject_r &block
  dup.reject_r!(&block)
end

#reject_r!(&block) ⇒ Object

Recursively filter this hash in-place to remove all key-value pairs for which the block returned true. That is, like Hash#reject!, except sub-hashes are also filtered.



78
79
80
81
82
83
84
85
86
# File 'lib/babushka/core_patches/hash.rb', line 78

def reject_r! &block
  each_pair {|k,v|
    if yield k, v
      self.delete k
    elsif v.is_a? Hash
      self[k] = v.reject_r(&block)
    end
  }
end

#selekt(&block) ⇒ Object

Return a new hash filtered to contain just the key-value pairs for which the block returns true. That is, like Hash#select, but returning a hash instead of an array of tuples.



60
61
62
63
64
65
66
# File 'lib/babushka/core_patches/hash.rb', line 60

def selekt &block
  hsh = {}
  each_pair {|k,v|
    hsh[k] = v if yield(k,v)
  }
  hsh
end

#slice(*keys) ⇒ Object

Return a new hash filtered to contain only the key-value pairs whose keys appear in keys.



17
18
19
# File 'lib/babushka/core_patches/hash.rb', line 17

def slice *keys
  dup.slice!(*keys)
end

#slice!(*keys) ⇒ Object

Filter this hash in-place so it contains only the key-value pairs whose keys appear in keys.



23
24
25
26
27
28
# File 'lib/babushka/core_patches/hash.rb', line 23

def slice! *keys
  keys.inject({}) {|acc,key|
    acc[key] = self.delete(key) if self.has_key?(key)
    acc
  }
end

#to_http_paramsObject

Converts this hash to a string that can be submitted as HTTP parameters. The keys and values are encoded, so the string is safe to submit exactly as it is returned.

This isn’t a recursive implementation, because I didn’t need it to be :)

Example: => ‘Sigur Rós’, ‘album’ => ‘Takk…’.to_http_params

#=> "name=Sigur%20R%C3%B3s&album=Takk..."


97
98
99
100
101
102
103
104
105
106
# File 'lib/babushka/core_patches/hash.rb', line 97

def to_http_params
  require 'uri'

  keys.map {|key|
    [
      URI.escape(key.to_s),
      URI.escape(fetch(key).to_s)
    ].join('=')
  }.join( '&')
end