Created
February 2, 2011 23:54
-
-
Save wmw/808768 to your computer and use it in GitHub Desktop.
Implementing a to_hash function for a class in ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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