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 trialKatarina Lingat
6,421 PointsWhy 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.
1 Answer
Alexander Davison
65,469 PointsI 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.
Katarina Lingat
6,421 PointsThank you!!
Alexander Davison
65,469 PointsNo problem :)
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsI always wondered the same thing. Tagging Jason Anders