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
Muhammad Saif
11,565 PointsWhat 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
Treehouse TeacherWhat 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:
-
kindandnumberare parameters to theadd_phone_numbermethod. -
phone_number.kind =calls thekind=method (yes, it's a method, I know that's weird) on thePhoneNumberinstance (object) stored in thephone_numbervariable. -
phone_number.number =calls thenumber=method on thePhoneNumberinstance (object) stored in thephone_numbervariable. - The
kind=andnumber=methods are attribute writer methods. When you call them, you set the@kindand@numberinstance variables on thePhoneNumberobject.
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.inspectshows thatphone_numbersis 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_numbersis an attribute reader method that should return an array. - Calling
push(phone_number)on that array should add thePhoneNumberobject to that array.
Muhammad Saif
11,565 PointsMuhammad Saif
11,565 PointsThank you! Your answer help me to understand more! And now I know that Jason is adding the PhoneNumber object to the array list!