Module: Utilities
- Defined in:
- lib/util/util.rb
Class Method Summary collapse
-
.deep_traverse_hash(obj, &block) ⇒ Object
Traverse each key, value of the hash and any nested hashes (including those in arrays) E.G.
- .remove_enclosing_quotes(str) ⇒ Object
Class Method Details
.deep_traverse_hash(obj, &block) ⇒ Object
Traverse each key, value of the hash and any nested hashes (including those in arrays) E.G. to deep transfrom do:
deep_traverse_hash(obj) { |hash, k, v| hash[k] = v.upcase if v.is_a?(String) }
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/util/util.rb', line 5 def self.deep_traverse_hash(obj, &block) if obj.is_a? Array obj.each { |val| deep_traverse_hash(val, &block) } elsif obj.is_a?(Hash) obj.each_pair do |k,v| deep_traverse_hash(v, &block) block.call(obj, k, v) end end end |
.remove_enclosing_quotes(str) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/util/util.rb', line 16 def self.remove_enclosing_quotes(str) quote_chars = ['"', '\''] quote_chars.each do |quote_char| return str[1..-2] if str.start_with?(quote_char) && str.end_with?(quote_char) end return str end |