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

Below the Address class definition, initialize a variable called address which will be a new instance of the Address cla

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.

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

  def initialize(kind, street_1, street_2, city, state, postal_code, address)
    @kind = kind or ''
    @street_1 = street_1 or ''
    @street_2 = street_2 or ''
    @city = city or ''
    @state = state  or ''
    @postal_code = postal_code or ''
    @address = address
  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

1 Answer

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 56,130 Points

HI Mustafa,

That wants you to create a new instance of Address, called address, and initialize it with the constructor:

address = Address.new("A building", "New Street", "Newtown", "London", "", "WC1 2AN")

I hope that helps,

Steve.