Ruby objects have no "properties." They have instance variables (private variables that store data for each instance) and methods that can access these variables.
Since brackets are optional in calls to the Ruby method, and since syntax sugar allows you to write obj.foo = 42
as a more convenient way to call the obj.foo=( 42 )
method, you might think that these are properties. They are not; they are methods indistinguishable from others.
This code:
class Foo attr_accessor :bar end f = Foo.new f.bar = 42 puts f.bar #=> 42
exactly the same as this (much longer) code:
class Foo def bar @bar end def bar=( val ) @bar = val end end f = Foo.new f.bar=( 42 ) puts( f.bar() )
The attr_*
methods actually create methods for you.
source share