Ruby Build an Address Book in Ruby Class Design Phone Number Class

Immo Struchholz
Immo Struchholz
10,515 Points

Weird output from printPhoneNumber method

This is the output I get from the printPhoneNumbers method:

Phone Numbers:
Kind: mobile, Number: 123456789
#<PhoneNumber:0x0055d1c17face0>
Kind: mobile, Number: 326136121
#<PhoneNumber:0x0055d1c17faba0>

What is going on here? It's the same method as in the video:

    def printPhoneNumbers
        puts "Phone Numbers:"
        phoneNumbers.each { |n| puts n }
    end

3 Answers

Chet Bramble
Chet Bramble
11,758 Points

Yup. You're fine leaving the colon in there. Your method, technically, is fine as well. Looks like you may need to review your to_s method.

Try:

def to_s
  "#{kind}: #{number}"
end

I'd also recommend staying with convention in regards to your naming of methods. By that I mean, stick with snake_case as opposed to camelCase.

Immo Struchholz
Immo Struchholz
10,515 Points

Thank you! My to_s method looked like this:

def to_s
    puts "Kind: #{kind}, Number: #{number}"
end

Removed the puts and now it works just fine.
Why is snake_case preferred? I only have experience with JavaScript and learned to use camelCase there.

Chet Bramble
Chet Bramble
11,758 Points

Hi Immo, try this:

def print_phone_numbers
  puts "Phone Numbers"
  phone_numbers.each { |phone_number| puts phone_number }
end
Immo Struchholz
Immo Struchholz
10,515 Points

Hi Chet, That's the exact same method as the one I wrote. Minus the colon after phone numbers. I tried it anyway, but it didn't work.

Chet Bramble
Chet Bramble
11,758 Points

I'm not positive why, other than it is the accepted way to do it by most Ruby programmers. My supposition is that it has to do with readability. Possibly to help differentiate from classes, which have CamelCase for their name's.