Why does Array#each return an array with the same elements?

Viewed 21465

I'm learning the details of how each works in ruby, and I tried out the following line of code:

p [1,2,3,4,5].each { |element| el }

And the result is an array of

[1,2,3,4,5]

But I don't think I fully understand why. Why is the return value of each the same array? Doesn't each just provide a method for iterating? Or is it just common practice for the each method to return the original value?

4 Answers

If you want, for some reason, to suppress the output (for example debugging in console) here is how you can achive that

  [1,2,3,4,5].each do |nr|
    puts nr.inspect
  end;nil
Related