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 trialMiles Smith
2,688 PointsAddress_book.rb
Hello Im working under address_book.rb in "building an address book", im getting an error that states says I have a error on line 27: in '<class:AddressBook>' : undefined local variable or method 'search' for AddressBook:Class (nameError) from address_book.rb: 3 in '<main>'
Code:
require "./contact"
class AddressBook attr_reader :contacts
def initialize @contacts = [] end def print_results(search, results) puts search results.each do |contact| puts contact.to_s('full_name') contact.print_phone_numbers contact.print_addresses puts "\n" end end def find_by_name(name) results = [] search = name.downcase contacts.each do |contact| if contact.full_name.downcase.include?(search) results.push(contact) end end end print_results("Contact search results (#{search})", results) end
def find_by_phone_number(number) results = [] search = number.gsub("-", "") contacts.each do |contact| contact.phone_numbers.each do |phone_number| if phone_number.number.gsub("-", "").include?(search) results.push(contact) end end end print_results("Phone search results (#{search})", results) end
def print_contact_list puts "Contact List" contacts.each do |contact| puts contact.to_s('last_first') end end address_book = AddressBook.new
jason = Contact.new jason.first_name = "Jason" jason.last_name = "Seifer" jason.add_phone_number("Home", "123-456-7890") jason.add_phone_number("Work", "456-789-0123") jason.add_address("Home", "123 Main St.", "", "Portland", "OR", "12345")
nick = Contact.new nick.first_name = "Nick" nick.last_name = "Pettit" nick.add_phone_number("Home", "222-222-2222") nick.add_address("Home", "222 Two Lane", "", "Portland", "OR", "12345")
address_book.contacts.push(jason) address_book.contacts.push(nick)
address_book.print_contact_list
address_book.find_by_name("e") address_book.find_by_phone_number("123")
Seth Reece
32,867 PointsWhen posting code with markdown be sure there is no empty lines between your opening 's and closing 's. Not sure if it's necessary, but I always put a blank line after my text before my code.
Miles Smith
2,688 PointsMiles Smith
2,688 PointsSorry for the terrible formatting after being posted, it showed up fine before I posted it here, I dont know how else to get workspaces code into the comments.