Skip to content

Instantly share code, notes, and snippets.

@3limin4t0r
Created October 28, 2019 20:23
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 3limin4t0r/cdf75f8f86e1836416adeb8d7182b19b to your computer and use it in GitHub Desktop.
Save 3limin4t0r/cdf75f8f86e1836416adeb8d7182b19b to your computer and use it in GitHub Desktop.
module AccessorA
def a
puts "getter a called, current value: #{@a.inspect}"
@a
end
def a=(a)
puts "setter a called, current value: #{@a.inspect}, new value: #{a.inspect}"
@a = a
end
end
obj = Object.new
obj.extend(AccessorA)
obj.a &&= 1
# getter a called, current value: nil
#=> nil
obj.a = 2
# setter a called, current value: nil, new value: 2
#=> 2
obj.a &&= 3
# getter a called, current value: 2
# setter a called, current value: 2, new value: 3
#=> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment