lib/lotus/utils/hash.rb in lotus-utils-0.3.1 vs lib/lotus/utils/hash.rb in lotus-utils-0.3.2
- old
+ new
@@ -55,15 +55,40 @@
end
self
end
+ # Convert in-place all the keys to Symbol instances, nested hashes are converted too.
+ #
+ # @return [Hash] self
+ #
+ # @since 0.3.2
+ #
+ # @example
+ # require 'lotus/utils/hash'
+ #
+ # hash = Lotus::Utils::Hash.new a: 23, b: { c: ['x','y','z'] }
+ # hash.stringify!
+ #
+ # hash.keys # => [:a, :b]
+ # hash.inspect # => {"a"=>23, "b"=>{"c"=>["x", "y", "z"]}}
+ def stringify!
+ keys.each do |k|
+ v = delete(k)
+ v = Hash.new(v).stringify! if v.is_a?(::Hash)
+
+ self[k.to_s] = v
+ end
+
+ self
+ end
+
# Return a deep copy of the current Lotus::Utils::Hash
#
# @return [Hash] a deep duplicated self
#
- # @since x.x.x
+ # @since 0.3.1
#
# @example
# require 'lotus/utils/hash'
#
# hash = Lotus::Utils::Hash.new(
@@ -250,10 +275,10 @@
@hash.respond_to?(m, include_private)
end
private
# @api private
- # @since x.x.x
+ # @since 0.3.1
def duplicate(value)
case value
when NilClass, FalseClass, TrueClass, Symbol, Numeric
value
when Hash