Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Build an address book with ruby.

Below the Address class definition, initialize a variable called address which will be a new instance of the Address class. Double check to make sure all required arguments are given.

Is this what they are asking for?

address.rb
class Address
  attr_accessor :kind, :street_1, :street_2, :city, :state, :postal_code
  address = Address.new("kind", "street_1", "street_2", "city,", "state", "postal_code") 
  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

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

Yeah, but you placed it INSIDE the class you want to instantiate. Move your code OUTSIDE the class.

Thanks!