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

imanewbie
imanewbie
9,139 Points

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

I am new to Ruby programming and programming in general but don't understand what is wrong with my code as is not accepted as an answer. I initialized new variable and gave it all arguments needed.

address.rb
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 ''
  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

address= Address.new
address.kind = 'home'
address.street_1 = 'random street'
address.street_2 = 'bum'
address.city = 'random city'
address.state = 'bum'
address.postal_code = '00-000'

2 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,809 Points

The error you receive is: Bummer: ArgumentError: wrong number of arguments (given 0, expected 6). To pass in an argument include it in parentheses following new.

address = Address.new('home', 'random street', etc.)
imanewbie
imanewbie
9,139 Points

Thank you. I got confused as I thought I can give arguments by using

variable_name.argument_type = "argument"

but it is wrong because attr_accessor turns instance variable into instance method