What is meant by this: def foo=(foo) ... end?

I saw in episode #75 that creating a method in the Product model will
update attributes related to a different model (Picture):

def picture_attributes=(picture_attributes)

end

I’m unclear as to the first line:

def picture_attributes=(picture_attributes)
vs.
def picture_attributes (picture_attributes)


Anthony E.
408-656-2473
http://anthony.ettinger.name

@pets = Pets.find_all_by_species(‘dog’)
@pets.each do |dog| {
if dog.name == ‘Farley’
dog.nick = ‘Sir Barks-A-lot’
elsif dog.name == ‘Bonita’
dog.nick = ‘Princess Boo’
else
dog.nick = ‘Doggie?’
end
}

On Dec 31, 2007, at 10:57 AM, Anthony E. wrote:

vs.
def picture_attributes (picture_attributes)

def picture_attributes=

defines the setter, whereas

def picture_attributes

defines the getter

It is unusual for a getter to take a parameter because getters are not
supposed to have side-effects, just return a value. More rubyish names
for these functions are “accessors” so declaring:

attr_reader :picture_attributes

automagically defines a getter
(class Module - RDoc Documentation
).

attr_writer :picture_attributes

automagically defines a getter that takes one value argument
(class Module - RDoc Documentation
).

attr_accessor :picture_attributes

automagically defines both
(class Module - RDoc Documentation
).