Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build an Address Book in Ruby Class Design Initializing and Calling Methods

Harrison Lim
Harrison Lim
2,309 Points

Don't really understand what is the question calling for

Don't really get what is being asked here. Hence I'm not quite sure where to begin.

On a side note, if there could be a hint button with pointers leading people in the right direction before having to post a question, that would be pretty great.

address.rb
class Address
  attr_accessor :kind, :street_1, :street_2, :city, :state, :postal_code

  def address
    address = ''
    address += kind
    address += street_1
    address += street_2 if !street_2.nil?
    address += city
    address += state
    address += postal_code
  end

  def initialize(kind, street_1, street_2, city, state, postal_code)
    @kind = kind or ''
    @street_1 = street_1 or ''
    @street_2 = street_2 or ''
    @city = city or ''
    @state = state  or ''
    @postal_code = postal_code or ''
  end

  def to_s(format = 'short')
    address = ''
    case format
    when 'long'
      address += street_1 + "\n"
      address += street_2 + "\n" if !street_2.nil?
      address += "#{city}, #{state} #{postal_code}"
    when 'short'
      address += "#{kind}: "
      address += street_1
      if street_2
        address += " " + street_2
      end
      address += ", #{city}, #{state}, #{postal_code}"
    end
    address
  end
end

3 Answers

Rick Buffington
Rick Buffington
8,146 Points

They are just wanting to make sure you know how to instantiate a new class with the correct number of parameters. They want you to call the class BELOW the definition of it (outside of the last end keyword). So, something like:

address = Address.new(ARGUMENTS HERE)

I left it slightly incomplete to give you a chance to make sure you understand how to call the class instantiate with the correct number of arguments based on the class definition. If you need more help let me know.

Harrison Lim
Harrison Lim
2,309 Points

Ah I see. I tried doing that but it threw an error message that said the 'address' variable was not defined. This is my code:

address = Address.new('home', 'BT Road', '', 'Singapore', 'SG', '279884')

Rick Buffington
Rick Buffington
8,146 Points

Did you put that at the very bottom of the exercise space? Like below the last end keyword? I copied and ran your command and it was a success. I placed it on line 32 at the very bottom.

Harrison Lim
Harrison Lim
2,309 Points

That's okay I figured it out. Gotta move it outside the class. Cheers

Rick Buffington
Rick Buffington
8,146 Points

Correct, that is what I was trying to say in my original post: "They want you to call the class BELOW the definition of it (outside of the last end keyword)" - glad you figured it out.