Ruby Build an Address Book in Ruby Input and Output Adding Contacts: Part 2

Jorge Rodriguez
Jorge Rodriguez
2,615 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,615 Points

Thanks loannis!!!

It was very helpful!