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

Patrick Mockridge
Patrick Mockridge
Full Stack JavaScript Techdegree Graduate 45,611 Points

What wrong with my code?

I create the new instance, I populate all the arguments. What's going wrong?

class Address
  attr_accessor :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 ''

  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
      address += ", #{city}, #{state}, #{postal_code}"
address = 
address.kind = "Home"
address.street_1 = "1"
address.street_2 = "Peace Drive" = "Peace City"
address.state = "Peaceland"
address.postal_code = "PEACE"

1 Answer

Eric Putnam
Eric Putnam
5,640 Points

intialize is called when you create a new instance, so the parameters must be passed to it just like any other function. so you might have something like

address ="Home", "1600 Pennsylvania Ave NW", "", "Washington", "DC", "123456")

Does that make sense?