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 Ruby Collections Build a Grocery List Program Build a Grocery List Program: Part 1

i don't know what 'return' does ?

it makes it available outside the method or something ?

3 Answers

I know how difficult it is to get your head around this stuff when you're starting out, so I'll try to explain it in 'normal' language as best I can.

When the create_list method is run a hash is created that contains two key/value pairs:

The 1st key/value pair consists of a key called: "name" and a value of: whatever name the user inputted when asked, "What is the list's name?".

The 2nd key/value pair consists of a key called: "items" and a value of: an empty array.

So, when the create_list method is run a hash is created called "hash" (it could have been called anything, but Jason chose "hash" - I actually found that quite confusing when I was learning this stuff).

Now then, the magical ethereal thing, which is your computer or Ruby or whatever, wants to 'see' is the RETURNED VALUE of the create_list method. When a method has finished running, it sends its final value into the magical ether i.e. it 'returns a value'. That value is basically the final value of whatever the last thing that happens inside that method is. This isn't always easy to work out (at least for mere mortals like me).

In the create_list method, a hash is created called "hash", with stuff in it, and then "return hash" is run. This ensures that when the create_list method is run, the 'returned value' of the method will be the hash called "hash" i.e. create_list() returns a hash called "hash".

Long-winded, but I hope that helps. It does all fall into place eventually, trust me.

Tommy Gebru
Tommy Gebru
30,164 Points
"Now then, the magical ethereal thing, which is your computer or Ruby or
whatever, wants to 'see' is the RETURNED VALUE of the create_list method. 
When a method has finished running, it sends its final value into the magical 
ether i.e. it 'returns a value'. 
That value is basically the final value of whatever the last thing that happens 
inside that method is. This isn't always easy to work out (at least for mere 
mortals like me).
In the create_list method, a hash is created called "hash", with stuff in it, and 
then "return hash" is run. 
This ensures that when the create_list method is run, the 'returned value' of 
the method will be the hash called "hash" i.e. create_list() returns a hash 
called "hash"."

I also like these references that treehouse students shared:

first found on Github

second on Ruby Docs

melissa brown
melissa brown
4,670 Points

Why is he using return here? Is it just so that we can see the value in the IRB. Or is it because he is creating a list and the value of the list has meet the criteria? It just read the link on stackoverflow in here. Some people have said that returns allows you to ‘break out earlier of a function’ or you can use it ‘when going through a list and you want to exit the function if any member of the list meets the criteria’. So what is his purpose for using return?

The line return hash could, instead, read hash and the code would do exactly the same thing. Jason has opted to use return here for readability.