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

**URL:** <https://www.ruby-forum.com/t/what-is-meant-by-this-def-foo-foo-end/124663>\
**Category:** Rails\
**Created:** [December 31, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-this-def-foo-foo-end/124663 "2007-12-31T19:58:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chovy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecd19e/32.png) [@chovy](https://www.ruby-forum.com/u/chovy)\
**Post date:** [December 31, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-this-def-foo-foo-end/124663/1 "2007-12-31T19:58:55Z")

</div>

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](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  
}

---

<div class="post-metadata">

**Author:** ![chovy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecd19e/32.png) [@chovy](https://www.ruby-forum.com/u/chovy)\
**Post date:** [December 31, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-this-def-foo-foo-end/124663/2 "2007-12-31T20:05:56Z")

</div>

> 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](http://ruby-doc.org/core/classes/Module.html#M001702)  
).

attr\_writer :picture\_attributes

automagically defines a getter that takes one value argument  
([class Module - RDoc Documentation](http://ruby-doc.org/core/classes/Module.html#M001703)  
).

attr\_accessor :picture\_attributes

automagically defines both  
([class Module - RDoc Documentation](http://ruby-doc.org/core/classes/Module.html#M001704)  
).
