Class: Rsubhak

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

Class Method Summary collapse

Class Method Details

.rsubhak(ha, k, p, r) ⇒ Object

Recursively traverse a hash or array ha, searching for all occurrences of hash key k, and substituting (within their values) all occurrences of pattern p for replacement r.

Parameters

ha

hash or array

k

hash key

p

pattern

r

replacement

Examples

rsubhak(params, 'price', /[€\.]/, '')
rsubhak(params, 'price', /,/, '.')

rsubhak(params, 'price', /[\$,]/, '')

Pronunciation

r sub hak



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rsubhak.rb', line 23

def self.rsubhak(ha, k, p, r)
  case ha
  when Hash
    ha[k].gsub!(p, r) if ha[k]
    ha.each { |key, value|
      case value
      when Hash
        rsubhak(ha[key], k, p, r)
      when Array
        rsubhak(value, k, p, r)
      end
    }
  when Array
    ha.each { |i|
      rsubhak(i, k, p, r) if i.class == Hash || i.class == Array
    }
  end
end