Skip to content

Instantly share code, notes, and snippets.

@zunda
Created January 4, 2016 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zunda/3623491b655bb251f1d0 to your computer and use it in GitHub Desktop.
Save zunda/3623491b655bb251f1d0 to your computer and use it in GitHub Desktop.
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
@zunda
Copy link
Author

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