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


Jimmy Mannan
Jimmy Mannan
5,201 Points

Why does .each return complete array as well.

Is there a way that .each prints only what is asked of it and not return the complete array set too at the end

my_array = ["clip", 23, 47, "bost"]
my_array.each do |q|
    puts {q}

this is what I get when i run it

clip 23 47 bost ["clip", 23, 47, "bost"]

Is it possible to not have the array set print in the end.

thanks jimmy

1 Answer

William Li
William Li
Courses Plus Student 26,868 Points

The return value of Array#each is the original Array, that's how it's defined in the Ruby api.

irb by default always prints out the return value of the expression, nil is outputted if there's no return value. However, if you prefer to suppress the return value output from irb, you can launch it by this command.

irb --noecho --simple-prompt

Now, irb won't echo the return value of an expression unless you explicitly tell it to by using the print, puts statement; I'm not sure that's how I wanna work with an interactive REPL environment like irb, but if you like it that way, it's good then.

Jimmy Mannan
Jimmy Mannan
5,201 Points

Yes I was running it on irb. now when i saved the file as .rb and ran on the ruby comment prompt, the output is four blank lines (confused emoticon).

I dont actually have a problem with the array set returning but just wanted to understand why it is happening. Thank you for explaining

Regards jimmy