Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

passing challenge 1 with a glitch, then unable to call to_s

part of my problem is a glitchy program. for challenge 1, the only way I pass is if i write a line at the bottom like, kind = kind, or street = street. which doesn't seem right, but without it, I don't pass. On the 2nd challenge, I can't seem to call to_s correctly on address. i can then delete kind = kind and not have it screw up the first challenge but kind of lost on where I screwed up? I have my doubts that challenge 1 is really correct at all.

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(:kind, :street_1, :street_2, :city, :state, :postal_code)
kind = kind

1 Answer

Ok, I made it work with:

address = Address.new('', '', '', '', '', '')

and on the second question:

address = Address.new('', '', '', '', '', '')
address.to_s

Now, I honestly don't know why what you did worked. The Challenge will use tests in the background to verify that answers meet certain requirements, and it looks like they failed to cover for an edge case here.

What you did was to assign a symbol to each of the class's instance variables. What I did was slightly different - I assigned empty strings to all of them. When you replicate this in irb, neither thing causes an error, so it will be something to do with how the tests have been written.

Why kind = kind I have no idea. Again, it might be to do with how the tests have been written.

Maybe staff will chime in. Unfortunately I don't know how to tag a staff member in this, but it should probably be flagged up.