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 Input and Output Adding Contacts: Part 2

Jorge Rodriguez
Jorge Rodriguez
2,616 Points

Don't fully understand; contact.phone_numbers.push(phone)

Hello everyone!

I'm trying to figure it out the following code:

contact.phone_numbers.push(phone)

I know the phone is an object that is being appended to an array of contact(another object), in this case the array is phone_numbers, i'm right?.

But i don't fully understand how?

Thanks people!

2 Answers

Ioannis Leontiadis
Ioannis Leontiadis
9,828 Points

Hello Jorge,

first of all keep in mind that Ruby arrays are not as rigid as arrays in other languages. They grow automatically when adding elements to them and are a kind of one-size-fits-all data structure.

So, in your code you are accessing contact's: object property phone_numbers which is an array. You could do something like,

myArray = contact.phone_numbers
myArray.push(phone)

which will had the same result. In the second line you are appending an element into the array. It can be of any type.

Note: Keep in mind that dot notation in Object-Oriented Programming is used for accessing the properties and methods of an object. The .push() function is actually a method of any array.

Hope that helped!

resources:

Jorge Rodriguez
Jorge Rodriguez
2,616 Points

Thanks loannis!!!

It was very helpful!