This question is about setter and getter methods in Ruby. In the example below, I have three methods. The first two are, respectively, to get and set an instance variable @login_method
. The third is an instance method that attempts to access the setter method. It fails to access the setter method, because when the line login_method = 15
is run, execution does not stop for the debugger statement I placed inside the setter method.
Why can I not access the setter method from inside of method mymeth
? I can access it if I prefix the setter method call with "self", but why do I have to do this?
require 'byebug'
class MyClass
def login_method
debugger;
@login_method
end
def login_method=(value)
debugger;
@login_method
end
def mymeth
debugger;
login_method = 15
end
end
obj = MyClass.new
obj.mymeth