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 trialwilliam miller
1,083 Pointsinquiring about Address Book challenge class design
I am inquiring about the way to complete the task for this challenge the way that Mr. Seifer does on his video. I set the @ address variable to = [ ] in initializing and set a method for add_address underneath. What more does this challenge need to be completed?
class Address
attr_accessor :kind, :street_1, :street_2, :city, :state, :postal_code
def initialize(kind, street_1, street_2, city, state, postal_code)
@address= []
def add_address(kind, street_1, street_2, city, state, postal_code)
address = Address.new
address.kind = kind
address.street_1 = street_1
address.street_2 = street_2
address.city = city
address.state = state
address.postal_code = postal_code
addresses.push(address)
end
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
Salman Akram
Courses Plus Student 40,065 PointsHi William,
The challenge question seemed to be vague and not clear to me. You can create new variable called address after class Address on the bottom.
We already have this in the challenge part, you don't have to change anything on initialize method. Let's take a look at initialize, you notice @kind, @street_1, @street_2, etc. That parts tell us to add address variable with Address class.
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
Example (add this on the bottom)
address = Address.new("Town", '342 Washington Road.', 'Crossroad Rd', 'Washington', 'DC', '45015')
Hope that helps :)
-Salman
william miller
1,083 Pointswilliam miller
1,083 PointsI guess we speak different languages because, I think that your answer is a little wordy. To be clear I wanted to know if the challenge called for me to write the address = address.new and add @address = [ ]. I gather that you are implying that I should not add anything new except address = address.new(.........) at the bottom of what? The initialize places or after the end or the bottom of the whole code......