Skip to content

Instantly share code, notes, and snippets.

@dbenhur
Created July 7, 2011 19:56
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbenhur/1070399 to your computer and use it in GitHub Desktop.
Save dbenhur/1070399 to your computer and use it in GitHub Desktop.
def transform_hash(original, options={}, &block)
options[:safe_descent] ||= {}
new_hash = {}
options[:safe_descent][original.object_id] = new_hash
original.inject(new_hash) { |result, (key,value)|
if (options[:deep] && Hash === value)
value = options[:safe_descent].fetch( value.object_id ) {
transform_hash(value, options, &block)
}
end
block.call(result,key,value)
result
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment