Skip to content

Instantly share code, notes, and snippets.

@toamitkumar
Created April 16, 2012 18:14
Show Gist options
  • Save toamitkumar/2400451 to your computer and use it in GitHub Desktop.
Save toamitkumar/2400451 to your computer and use it in GitHub Desktop.
class Product
def initialize
@name = "Ruby Book"
@price = 20.65
end
end
product = Product.new
hash = {}
product.instance_variables.each {|attr| hash[attr.to_s.delete("@")] = product.instance_variable_get(attr) }
p hash # => {"name"=>"Ruby Book", "price" => 20.65}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment