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 trialMuhammad 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:
-
kind
andnumber
are parameters to theadd_phone_number
method. -
phone_number.kind =
calls thekind=
method (yes, it's a method, I know that's weird) on thePhoneNumber
instance (object) stored in thephone_number
variable. -
phone_number.number =
calls thenumber=
method on thePhoneNumber
instance (object) stored in thephone_number
variable. - The
kind=
andnumber=
methods are attribute writer methods. When you call them, you set the@kind
and@number
instance variables on thePhoneNumber
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 thatphone_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 thePhoneNumber
object 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!