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

how does this work

hi everyone, I was wondering how this line of code list['items'].push(add_list_item()), prints in the console if we don't even put print or puts in front of it. Also, what is the difference between puts and print

2 Answers

John Steer-Fowler
PLUS
John Steer-Fowler
Courses Plus Student 11,734 Points

Hi Aaron,

I don't think this line of code is printing to the console in the traditional sense of print.

I believe this code will give some output to the console just to show you the outcome of the array now you have pushed an item into the array.

With regards to the difference between puts and print. Both of these print something to the screen, but puts prints a newline after what is printed out, whereas print does not.

I hope this helps

Francois Martel
Francois Martel
24,694 Points

As I understand it, the return value of the push() method is the new array length after the value(s) included as parameters to that method have been added to the array. Some methods have return values that print something to the console, even if they are not parameters in a puts or print statement themselves. The "print" method is just the basic way to make some content (string, numbers, etc) appear on the console, the "puts" method also displays content you pass it as a parameter but with an additional new line added after the printed text. Hope this helps!