lib/planter/hash.rb in planter-cli-3.0.2 vs lib/planter/hash.rb in planter-cli-3.0.3
- old
+ new
@@ -1,9 +1,33 @@
# frozen_string_literal: true
module Planter
## Hash helpers
class ::Hash
+ ## Turn all keys and values into string
+ ##
+ ## @return [Hash] copy of the hash where all its keys are strings
+ ##
+ def stringify
+ each_with_object({}) do |(k, v), hsh|
+ hsh[k.to_s] = if v.is_a?(Hash) || v.is_a?(Array)
+ v.stringify
+ else
+ v.to_s
+ end
+ end
+ end
+
+ ## Destructive version of #stringify
+ ##
+ ## @return [Hash] Hash with stringified keys and values
+ ##
+ ## @see #stringify
+ ##
+ def stringify!
+ replace stringify
+ end
+
## Turn all keys into string
##
## @return [Hash] copy of the hash where all its keys are strings
##
def stringify_keys