Skip to content

Instantly share code, notes, and snippets.

@wmw
Created February 2, 2011 23:54
Show Gist options
  • Save wmw/808768 to your computer and use it in GitHub Desktop.
Save wmw/808768 to your computer and use it in GitHub Desktop.
Implementing a to_hash function for a class in ruby.
class Balls
def initialize()
@name = 'blah'
@caption = 'blah'
end
def to_hash
hash = self.instance_variables.inject({}) do |a, e|
hash = {e[1..-1] => self.instance_variable_get(e)}
a.merge(hash)
end
hash.symbolize_keys!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment