Last active
March 5, 2016 17:56
-
-
Save scott-ad-riley/5e6783749f9fbdae4b0c to your computer and use it in GitHub Desktop.
Hash of Object Instance Variables (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 myClass | |
# [initialize with instance variable assignments] | |
def to_hash | |
return Hash[self.instance_variables.map{|var_name| [var_name.to_s.delete("@"), eval(var_name.to_s)]}] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment