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 Phone Number Class

Muhammad Saif
Muhammad Saif
11,565 Points

What is the purpose of these line written in add_phone_number(kind, number) method?

In the add_phone_number method Jason write these lines.

  def add_phone_number(kind, number)
    phone_number = PhoneNumber.new
    phone_number.kind = kind
    phone_number.number = number
    phone_numbers.push(phone_number)
  end

What is the meaning and purpose of adding these two lines?

    phone_number.kind = kind
    phone_number.number = number

And by adding

    phone_numbers.push(phone_number)

How does ruby know to interpret it as an array (phone_numbers.inspect shows that phone_numbers is an instance object which is not an array) as Jason uses the each method as below

  def print_phone_numbers
    puts "Phone Numbers: "
    phone_numbers.each {|phone_number| puts phone_number}
  end

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

What is the meaning and purpose of adding these two lines? phone_number.kind = kind; phone_number.number = number

In this code:

  def add_phone_number(kind, number)
    phone_number = PhoneNumber.new
    phone_number.kind = kind
    phone_number.number = number
    phone_numbers.push(phone_number)
  end

Basically, you're setting the kind and number attributes on a PhoneNumber object. In detail:

  • kind and number are parameters to the add_phone_number method.
  • phone_number.kind = calls the kind= method (yes, it's a method, I know that's weird) on the PhoneNumber instance (object) stored in the phone_number variable.
  • phone_number.number = calls the number= method on the PhoneNumber instance (object) stored in the phone_number variable.
  • The kind= and number= methods are attribute writer methods. When you call them, you set the @kind and @number instance variables on the PhoneNumber object.

So if I call add_phone_number("Home", "480-555-1212"), it first creates a PhoneNumber object with a kind of "Home" and a number of "480-555-1212".

And by adding phone_numbers.push(phone_number) How does ruby know to interpret it as an array (phone_numbers.inspect shows that phone_numbers is an instance object which is not an array)

Hmm, I'm a little surprised to hear that it doesn't show up as an array. Are you sure phone_numbers.class doesn't return Array? About the only way the push method would work is if it was an Array. Again, in detail, what I think is happening is this:

  • phone_numbers is an attribute reader method that should return an array.
  • Calling push(phone_number) on that array should add the PhoneNumber object to that array.
Muhammad Saif
Muhammad Saif
11,565 Points

Thank you! Your answer help me to understand more! And now I know that Jason is adding the PhoneNumber object to the array list!