Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
class Dog
def name=(x)
trace_ivar_set(:@name, x)
end
def trace_ivar_set(ivar, x)
puts "#{ivar} is set to #{x.inspect} for #{self.inspect} at #{caller[1]} called from #{caller[2]}"
instance_variable_set(ivar, x)
end
def name_taro
self.name = 'Taro'
end
end
a = Dog.new
a.name_taro
p a
Owner

zunda commented Jan 4, 2016

$ ruby dog.rb
@name is set to "Taro" for #<Dog:0x007fbf3c2861c8> at dog.rb:12:in `name_taro' called from dog.rb:17:in `<main>'
#<Dog:0x007fbf3c2861c8 @name="Taro">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment