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 Accessing Items in Arrays

I've been stuck on this one and I need some help.

I've been defining the variable first_item and putting grocery_list [0] = first_item then end.

array.rb
grocery_list = ["milk", "eggs", "bread", "ice cream", "potatoes", "pie"]

def first_item
  grocery_list[0] = first_item 

end

1 Answer

Angela Visnesky
Angela Visnesky
20,927 Points

Hi William! You are making things a little more complicated than you need to.

first_item = grocery_list[0]

This will put you on the path to finishing the challenge. I hope this helps!

Scott Wyngarden
Scott Wyngarden
Courses Plus Student 16,700 Points

And to add a little more information, when you're assigning to a variable, the variable goes on the left side of the equals sign, and the value (or where you're getting the value from) goes on the right. so given a variable called my_variable, any time you want to assign to it, it your code should look something like this

my_variable = 5

or

my_variable = grocery_list[0]

The code you wrote tried assigning whatever first_item holds to the first element of your array. In short, you just had your syntax backwards

Hey thanks for the help I appreciate it.