classTestClassattr_accessor:onedefmy_method(branch=true)ifbranchputs"Do nothing to modify `one`"elseputs"Modify `one` but it's a local variable"one="test"endone# local variableenddefmy_non_modifying_method(branch=true)ifbranchputs"Do nothing to modify `one`"elseputs"Do nothing to modify `one` either"endone#method callendend
1234567891011
o=TestClass.newo.one="Value"putso.my_method=>nil#might expect 'Value' if you're not paying attentionputso.my_non_modifying_method#expects "Value"=>"Value"putso.my_method(false)=>"test"putso.my_non_modifying_method(false)#expects "Value"=>"Value"
So remember, if you create any local variables anywhere in your method, even if they’re not called, they override the accessor methods and will give you results you’re not expecting. To get around it, make sure you always use self.accessor= to assign values when there is ambiguity.