getter and setter
class TestSetterGetter
attr_accessor :something
def any_method
self.something = "abc"
#setter inside class, instead of @something
puts something
#getter inside class
end
end
test = TestSetterGetter.new
test.something = "test"
# setter
puts test.something
# getter
#=> "test"
test.any_method
#=> "abc"
Reference
- Why isn't self always needed in ruby / rails / activerecord?
- Ruby/Rails: Understanding ruby getter-setter methods and instances
- 2.0 Instance Variables and Accessors
- Ruby on Rails 3: Setters and Getters and self inside a controller method
- attr_accessor vs attr_reader & instance variables
- Difference between @instance_variable and attr_accessor
- Does Ruby's attr_accessor automatically create instance variables for attributes?
- Private Accessors in Ruby
- The Elements of Style in Ruby #6: Attributes Redux