# Not sure why this is failing, barring a typo:

**URL:** <https://www.ruby-forum.com/t/not-sure-why-this-is-failing-barring-a-typo/158571>\
**Category:** Ruby\
**Created:** [January 25, 2009, 2:00am UTC](https://www.ruby-forum.com/t/not-sure-why-this-is-failing-barring-a-typo/158571 "2009-01-25T02:00:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuckysocks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/ba8739/32.png) [@yuckysocks](https://www.ruby-forum.com/u/yuckysocks)\
**Post date:** [January 25, 2009, 2:00am UTC](https://www.ruby-forum.com/t/not-sure-why-this-is-failing-barring-a-typo/158571/1 "2009-01-25T02:00:46Z")

</div>

This code, directly from a book:

class Animal  
def initialize(color)  
@color = color  
end

def get\_color  
return @color  
end  
end

animal = Animal.new(“brown”)  
puts "The new animal is " + animal.color

results in “undefined method: color”. But aren’t I defining the method  
as part of the constructor? Maybe I just don’t understand what I’m  
doing… anyhow, help would be appreciated! 🙂

-Alex

---

<div class="post-metadata">

**Author:** ![yuckysocks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/ba8739/32.png) [@yuckysocks](https://www.ruby-forum.com/u/yuckysocks)\
**Post date:** [January 25, 2009, 2:08am UTC](https://www.ruby-forum.com/t/not-sure-why-this-is-failing-barring-a-typo/158571/2 "2009-01-25T02:08:22Z")

</div>

On Sat, Jan 24, 2009 at 7:58 PM, yuckysocks [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end
> 
> animal = Animal.new(“brown”)  
> puts "The new animal is " + animal.color
> 
> results in “undefined method: color”. But aren’t I defining the method  
> as part of the constructor? Maybe I just don’t understand what I’m  
> doing… anyhow, help would be appreciated! 🙂

Your method is named get\_color, you’re trying to call the method  
color. Make sure they match and it should work.

HTH,  
Michael G.

---

<div class="post-metadata">

**Author:** ![yuckysocks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/ba8739/32.png) [@yuckysocks](https://www.ruby-forum.com/u/yuckysocks)\
**Post date:** [January 25, 2009, 2:11am UTC](https://www.ruby-forum.com/t/not-sure-why-this-is-failing-barring-a-typo/158571/3 "2009-01-25T02:11:52Z")

</div>

> end
> 
> animal = Animal.new(“brown”)  
> puts "The new animal is " + animal.color
> 
> results in “undefined method: color”. But aren’t I defining the method  
> as part of the constructor? Maybe I just don’t understand what I’m  
> doing… anyhow, help would be appreciated! 🙂
> 
> -Alex

No.  
@color = color  
just defines instance variable, which can’t be accessed from outside.  
You must define reader method:

attr\_reader :color

or

def color  
return @color  
end

–  
“Configure complete, now type ‘make’ and PRAY.”

```
            (configure script of zsnes - www.zsnes.com)
```
