Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
to_hash recursive for HashWithIndifferentAccess
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
@pacojp

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
@qichunren

@pacojp works perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.