Skip to content

Instantly share code, notes, and snippets.

@RyanScottLewis
Created November 23, 2010 08:34
Show Gist options
  • Select an option

  • Save RyanScottLewis/711462 to your computer and use it in GitHub Desktop.

Select an option

Save RyanScottLewis/711462 to your computer and use it in GitHub Desktop.
Array#to_h, Struct#to_h, and Struct#to_json
require 'json'
class Array
def to_h
inject({}) { |memo, kv| key, value = *kv; memo[key] = value; memo }
end
end
class Struct
def to_h
each_pair.to_a.to_h
end
def to_json
to_h.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment