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)


I’m unclear as to the first line:

def picture_attributes=(picture_attributes)
def picture_attributes (picture_attributes)

Anthony E.

@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’
dog.nick = ‘Doggie?’

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

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

attr_writer :picture_attributes

automagically defines a getter that takes one value argument

attr_accessor :picture_attributes

automagically defines both

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs