# Why don't these writable atributes work?

**URL:** <https://www.ruby-forum.com/t/why-dont-these-writable-atributes-work/68961>\
**Category:** Ruby\
**Created:** [September 17, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/why-dont-these-writable-atributes-work/68961 "2006-09-17T14:31:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![edward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@edward](https://www.ruby-forum.com/u/edward)\
**Post date:** [September 17, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/why-dont-these-writable-atributes-work/68961/1 "2006-09-17T14:31:47Z")

</div>

Both of these code examples for making class properties don’t work  
although they are pretty much straight out of the book “Programming  
Ruby”. What I am doing wrong here?

## error is: undefined method `firstName’ for #\<User:0xb7f76b24 @firstName=“Newton”\> (NoMethodError)

class User

```
attr_writer :firstName

def initialize(firstName)
	@firstName = firstName
end

```

end

## user = User.new(‘Hal’) user.firstName = ‘Newton’ print user.firstName

class User

```
def firstName=(newFirstName)
	@firstName = newFirstName
end

def initialize(firstName)
	@firstName = firstName
end

```

end

user = User.new(‘Hal’)  
user.firstName = ‘Newton’  
print user.firstName

* * *

Thanks,

Edward Tanguay  
All my projects: [http://www.tanguay.info](http://www.tanguay.info)

---

<div class="post-metadata">

**Author:** ![edward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@edward](https://www.ruby-forum.com/u/edward)\
**Post date:** [September 17, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/why-dont-these-writable-atributes-work/68961/2 "2006-09-17T14:35:49Z")

</div>

Edward wrote:

> Both of these code examples for making class properties don’t work  
> although they are pretty much straight out of the book “Programming  
> Ruby”. What I am doing wrong here?
> 
> ## error is: undefined method `firstName’ for #\<User:0xb7f76b24 @firstName=“Newton”\> (NoMethodError)

you didn’t create an attribute reader:

attr\_accessor :firstName - creates both reader and writer

attr\_writer :firstName - creates a writer

attr\_reader :firstName - creates a reader (the one that you need)

You can also create a reader by hand:

def firstName  
@firstName  
end

lopex

---

<div class="post-metadata">

**Author:** ![edward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@edward](https://www.ruby-forum.com/u/edward)\
**Post date:** [September 17, 2006, 2:37pm UTC](https://www.ruby-forum.com/t/why-dont-these-writable-atributes-work/68961/3 "2006-09-17T14:37:49Z")

</div>

On 17/09/06, Edward [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class User  
> user = User.new(‘Hal’)

‘attr\_writer :firstName’ only creates the ‘firstName=’ method just  
like ‘attr\_reader :firstName’ will only create the ‘firstName’ method.  
What you need is ‘attr\_accessor :firstName’ which creates both.

Farrel

---

<div class="post-metadata">

**Author:** ![edward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@edward](https://www.ruby-forum.com/u/edward)\
**Post date:** [September 17, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/why-dont-these-writable-atributes-work/68961/4 "2006-09-17T16:05:56Z")

</div>

Edward wrote:

> Both of these code examples for making class properties don’t work  
> although they are pretty much straight out of the book “Programming  
> Ruby”. What I am doing wrong here?
> 
> ## error is: undefined method `firstName’ for #\<User:0xb7f76b24 @firstName=“Newton”\> (NoMethodError)
> 
> class User
> 
> attr\_writer :firstName

Just an additional note: the usual conventions for method and variable  
identifiers in Ruby is to separate words by underscore and use camel  
case only for class and module names. So that would rather be

class User  
attr\_accessor :first\_name  
end

Kind regards

```
robert
```
