public

to_hash recursive for HashWithIndifferentAccess

  • Download Gist
hash_with_indifferent_access_extension.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13
class HashWithIndifferentAccess < Hash
def to_hash_recursive
result = self.to_hash
 
result.each do |key, value|
if(value.is_a? HashWithIndifferentAccess)
result[key] = value.to_hash_recursive
end
end
 
result
end
end

how about this?

class Hash
  def to_hash_recursive
    result = self.to_hash

    result.each do |key, value|
      case value
      when Hash
        result[key] = value.to_hash_recursive
      when Array
        result[key] = value.to_hash_recursive
      end
    end

    result
  end
end

class Array
  def to_hash_recursive
    result = self

    result.each_with_index do |value,i|
      case value
      when Hash
        result[i] = value.to_hash_recursive
      when Array
        result[i] = value.to_hash_recursive
      end
    end

    result
  end
end

@pacojp works perfect!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.