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

J V
PRO
J V
Pro Student 16,607 Points

Initialize variable address

"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."

But nothing's working for me, so I haven't figured out Treehouse special way to write it yet. Idea? :)

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

5 Answers

Vlad Filiucov
Vlad Filiucov
10,665 Points

You need to create a new instance of address class below the class. Type in something like this at the end of the page

address = Address.new("short", "GAgarin", "blabla", "Chisinau,", "Chisinau", "md-2004")

John Steer-Fowler
PRO
John Steer-Fowler
Pro Student 11,732 Points

Yep, try something like this:

address = Address.new('Home', '123 Main St.', 'Main Rd.', 'Portland', 'OR', '12345')

as in the video...

In Ruby, when a question asks you to check 'arguments', these is the information that was pre-initialized into the Address class (see the code that the challenge provides you with).

La Kenzi
La Kenzi
14,346 Points

I am currently having the same problem clicked on your post in hopes that someone has helped us. But no luck yet, although I am currently working my way through the Ruby tracks it looks like you already fought through most of the problems. Would you be able to shed some insight or even help in your free time?

J V
J V
Pro Student 16,607 Points

I have looked at that video several times so I'm pretty sure there's nothing wrong with my logic. It might be the platform. lol

I hope someone does answer.

I will be glad to help however I can.

La Kenzi
La Kenzi
14,346 Points

Oh Yeas!!! Thank you so much that worked.

Task 1 at the bottom of page 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("kind", "street_1", "street_2", "city,", "state", "postal_code")