Newb question: Printing a class.

Discussion in 'Ruby' started by Brian A., Aug 28, 2008.

  1. Brian A.

    Brian A. Guest

    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

    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/) 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) 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian A., Aug 28, 2008
    #1
    1. Advertising

  2. 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).
    --
    Posted via http://www.ruby-forum.com/.
     
    Shashank Agarwal, Aug 28, 2008
    #2
    1. Advertising

  3. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Matthias Reitinger, Aug 28, 2008
    #3
  4. Brian A.

    Brian A. Guest

    Shashank Agarwal & Matthias Reitinger 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!
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian A., Aug 28, 2008
    #4
  5. Brian A.

    Adam Shelly Guest

    On 8/28/08, Brian A. <> wrote:
    > Shashank Agarwal & Matthias Reitinger 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?
    >

    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
     
    Adam Shelly, Aug 28, 2008
    #5
  6. Brian A.

    Brian A. Guest

    Adam Shelly wrote:
    > On 8/28/08, Brian A. <> 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!
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian A., Aug 28, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,955
    Thomas Weidenfeller
    Oct 12, 2005
  2. Knoppix User
    Replies:
    3
    Views:
    305
    Knoppix User
    Jan 11, 2004
  3. johnny
    Replies:
    3
    Views:
    341
    Duncan Booth
    May 11, 2007
  4. Shaun Fanning

    Variable class (newb question)

    Shaun Fanning, Jul 20, 2005, in forum: Ruby
    Replies:
    5
    Views:
    140
    Shaun Fanning
    Jul 20, 2005
  5. Art Gillespie
    Replies:
    5
    Views:
    145
    Robert Klemme
    Oct 8, 2005
Loading...

Share This Page