Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Collections Ruby Arrays Adding Items to Arrays

Why do I need to do `puts grocery_list.inspect` ? Why can't I just do `puts grocery_list`?

I tried both, and they both seem to do the same...show me the array of grocery_list.

I always wondered the same thing. Tagging Jason Anders

1 Answer

I found the answer by looking it up in the Ruby documentation.

.inspect() Is basically the same thing as .to_s. They convert arrays into strings.

If you call puts on an array itself, for example:

my_list = [1, 2, 3]
puts my_list

It will print:

1
2
3

However, if you first inspect the array, it will look like:

my_list = [1, 2, 3]
puts my_list.inspect

It will print:

[1, 2, 3]

As you see, with inspect, it will print the array raw. Without it will print it in the pretty format.

Thank you!!

No problem :)