module DeepMap
Public Instance Methods
deep_map() { |x| ... }
click to toggle source
# File lib/deepmap/deepmap.rb, line 4 def deep_map return self if !block_given? recurse { |x| yield x } end
key_map() { |x| ... }
click to toggle source
# File lib/deepmap/deepmap.rb, line 9 def key_map return self if !block_given? recurse("key") { |x| yield x } end
val_map() { |x| ... }
click to toggle source
# File lib/deepmap/deepmap.rb, line 14 def val_map return self if !block_given? recurse("val") { |x| yield x } end
Private Instance Methods
hash_collect(t, h)
click to toggle source
# File lib/deepmap/deepmap.rb, line 42 def hash_collect(t, h) Hash[h.collect { |k, v| case t # type when "both", "key" [yield(k), recurse(t, v) { |x| yield x }] when "val" [k, recurse(t, v) { |x| yield x }] end }] end
recurse(t = "both", h = self) { |x| ... }
click to toggle source
Main recursive method. With the block passed in, keep recursing through the object, applying the block to either each key or value (or both) depending on the first caller of this method. This is the value stored in 't' type. If the key is a hash, than collect the results of calling it recursively. If the key is an array, then map the recursive method over each item in the array. If it is niether a hash or an array, then apply the block to that item, and return the result.
# File lib/deepmap/deepmap.rb, line 29 def recurse(t = "both", h = self) if h.is_a?(Hash) hash_collect(t, h) { |x| yield x } elsif h.is_a?(Array) h.map { |v| recurse(t, v) { |x| yield x } } else # apply to value t == "key" ? h : yield(h) end end