Class: Rsubhak
- Inherits:
-
Object
- Object
- Rsubhak
- Defined in:
- lib/rsubhak.rb
Class Method Summary collapse
-
.rstrip(ha) ⇒ Object
Recursively traverse a hash or array ha (potentially of other nested hashes and/or arrays), searching for all values that respond to strip, and calling strip! on them to remove leading and trailing whitespace.
-
.rsubhak(ha, k, p, r) ⇒ Object
Recursively traverse a hash or array ha (potentially of other nested hashes and/or arrays), searching for all occurrences of hash key k, and substituting (within their values) all occurrences of pattern p for replacement r.
Class Method Details
.rstrip(ha) ⇒ Object
Recursively traverse a hash or array ha (potentially of other nested hashes and/or arrays), searching for all values that respond to strip, and calling strip! on them to remove leading and trailing whitespace.
Parameters
- ha
hash or array
Examples
rstrip(params)
Pronunciation
r strip
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rsubhak.rb', line 57 def self.rstrip(ha) case ha when Hash ha.each { |key, value| case value when Hash self.rstrip(ha[key]) when Array self.rstrip(value) else value.strip! if value.respond_to?(:strip) # base case end } when Array ha.each_with_index { |item, index| if item.class == Hash || item.class == Array self.rstrip(item) elsif item.respond_to?(:strip) # base case ha[index].strip! end } end end |
.rsubhak(ha, k, p, r) ⇒ Object
Recursively traverse a hash or array ha (potentially of other nested hashes and/or arrays), 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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rsubhak.rb', line 24 def self.rsubhak(ha, k, p, r) case ha when Hash ha[k].gsub!(p, r) if ha[k] # base case 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 |