# Newb question: Printing a class

**URL:** <https://www.ruby-forum.com/t/newb-question-printing-a-class/147139>\
**Category:** Ruby\
**Created:** [August 28, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/newb-question-printing-a-class/147139 "2008-08-28T19:04:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 28, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/newb-question-printing-a-class/147139/1 "2008-08-28T19:04:46Z")

</div>

Hello, Im new to the forum and new to Ruby, but I have  
programmed before. Im reading through this tutorial as my first venture  
into Ruby: [http://www.math.umd.edu/~dcarrera/ruby/0.3/index.html](http://www.math.umd.edu/~dcarrera/ruby/0.3/index.html)

I apologize if this is the wrong forum or I break any forum rules in  
this post, please let me know if I do!

I am using Eclipse + RDT ([http://rubyeclipse.sourceforge.net/](http://rubyeclipse.sourceforge.net/)) for my  
IDE.

I am currently on this page of the tutorial  
([http://www.math.umd.edu/~dcarrera/ruby/0.3/chp\_04/classes2.html](http://www.math.umd.edu/~dcarrera/ruby/0.3/chp_04/classes2.html)) and I  
am having a performing the exercise at the bottom of the page.

Here is the code I have so far:

========================================================================  
class Address  
attr\_accessor :street, :city, :state, :zip  
def initialize  
@street = @city = @state = @zip = “”  
end

```
def to_s
    @street + "\n" + \
    @city + "\n" + \
    @state + ", " + @zip
end

```

end

class Person  
attr\_accessor :first\_name, :email  
attr\_accessor :last\_name, :address  
def initialize  
@first\_name = @last\_name = @email = “”  
@address = Address.new  
end

```
def full_name
    @first_name + " " + @last_name
end

def to_s
    @first_name + " " + @last_name + "\n" + \
    @email + "\n"
end

```

end

sandy\_addr = Address.new  
sandy\_addr.street = “324 Campus Dr.”  
sandy\_addr.city = “College Park”  
sandy\_addr.state = “CO”  
sandy\_addr.zip = “55555”

# sandy = Person.new sandy.first\_name = “Sandy” sandy.last\_name = “Kohh” sandy.address = sandy\_addr

If I then run: ‘puts sandy.address’ it correctly outputs the address.  
This bit of code was given to me in the tutorial. However if I type  
‘puts sandy.person’ I get the following error message:  
‘test\_temp1.rb:43: undefined method `person’ for #Person:0x2f2324c  
(NoMethodError)’

I am lost because the def to\_s inside the Person class looks exactly  
like the def to\_s in the Address class. What am I missing or doing  
wrong? Thanks in advance!

-Brian

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 28, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/newb-question-printing-a-class/147139/2 "2008-08-28T19:17:14Z")

</div>

Brian A. wrote:

> This bit of code was given to me in the tutorial. However if I type  
> ‘puts sandy.person’ I get the following error message:  
> ‘test\_temp1.rb:43: undefined method `person’ for #Person:0x2f2324c  
> (NoMethodError)’

This is because you are asking it to find the return value of method  
‘person’, but such a method does not exist. With sandy.address, you  
asked for the return value of method ‘address’. This basically was  
returning the object ‘address’ for sandy. To get the to\_s method in  
person, just put -

puts sandy

That should work (not tested).

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 28, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/newb-question-printing-a-class/147139/3 "2008-08-28T19:18:04Z")

</div>

Hello Brian,

Brian A. wrote:

> If I then run: ‘puts sandy.address’ it correctly outputs the address.  
> This bit of code was given to me in the tutorial. However if I type  
> ‘puts sandy.person’ I get the following error message:  
> ‘test\_temp1.rb:43: undefined method `person’ for #Person:0x2f2324c  
> (NoMethodError)’

There is no method called `person` in your Person class. If you want to  
print the string representation of `sandy`, just do a `puts sandy`.

Regards,  
Matthias

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 28, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/newb-question-printing-a-class/147139/4 "2008-08-28T19:28:46Z")

</div>

Shashank A. & Matthias R. wrote:

> puts sandy

Thank you both that did work, but opened a new issue for me.

When I change the def to\_s within the Person class to this (adding the  
@address):

# ======================================================================== def to\_s @first\_name + " " + @last\_name + “\n” + @email + “\n” @address end

I get this “#Person:0x2b3340c”, is this because the Person class makes  
reference to the Address class, and its the address class has its own  
to\_s that prints? Should I move the @street + “\n” + , …etc to the  
Person class from the Address class?

If I change the def a little to this:

# ======================================================================== def to\_s @first\_name + " " + @last\_name + “\n” + @email + “\n” @address + “\n” end

I get a different error message:  
“test\_temp1.rb:25:in `to_s': undefined method`+’ for  
#Address:0x3413f68 (NoMethodError)  
from test\_temp1.rb:41:in `puts’  
from test\_temp1.rb:41”

Thanks!

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 28, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/newb-question-printing-a-class/147139/5 "2008-08-28T19:51:09Z")

</div>

On 8/28/08, Brian A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ========================================================================  
> Person class from the Address class?

You are right, the Address class does have its own #to\_s. The issue  
here is that when you type @address, you are not automatically calling  
it. You need to do @address.to\_s. (The puts method does  
automatically call #to\_s on its arguments, but it is not recursive).

Also, you probably want a ‘+’ after the @email + “\n”. Methods return  
the value of the last line, so without you will only return the  
address, not the name and email from the previous lines.

-Adam

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 28, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/newb-question-printing-a-class/147139/6 "2008-08-28T20:10:03Z")

</div>

Adam S. wrote:

> On 8/28/08, Brian A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ========================================================================  
> > Person class from the Address class?
> 
> You are right, the Address class does have its own #to\_s. The issue  
> here is that when you type @address, you are not automatically calling  
> it. You need to do @address.to\_s. (The puts method does  
> automatically call #to\_s on its arguments, but it is not recursive).
> 
> Also, you probably want a ‘+’ after the @email + “\n”. Methods return  
> the value of the last line, so without you will only return the  
> address, not the name and email from the previous lines.
> 
> -Adam

Thank you, that worked great!
