Created
September 12, 2014 18:47
-
-
Save kisai/f2459abfff7381883674 to your computer and use it in GitHub Desktop.
Convert object to hash 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 Gift | |
def initialize | |
@name = "book" | |
@price = 15.95 | |
end | |
end | |
gift = Gift.new | |
hash = {} | |
gift.instance_variables.each {|var| hash[var.to_s.delete("@")] = gift.instance_variable_get(var) } | |
p hash # => {"name"=>"book", "price"=>15.95} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment