Wednesday, 31 July 2013

instance_values

:instance_variables
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}
:attributes.to_options
App.first.attributes.to_options
:instance_variable_get
class Fred
def initialize(p1, p2)
@a, @b = p1, p2
end
end
fred = Fred.new('cat', 99)
fred.instance_variable_get(:@a) #=> "cat"
fred.instance_variable_get("@b") #=> 99
:instance_values
Gift.new.instance_values # => {"name"=>"book", "price"=>15.95}
:attributes
App.first.attributes
view raw instance_values hosted with ❤ by GitHub

No comments:

Post a Comment