Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
.each iteration - why am i getting double printings
3 Posts
.each iteration - why am i getting double printings


I'm starting with rails, i'm reading some books and watching EDX saas course.

I have a question here, when i'm into the irb interpreter or building some web app in rails and i create some iteration with '.each' i'm getting the whole struct printed again, in my terminal or inside the browser. I'm using ruby 1.9.3 and last rails version.


a = [1,2,3,4]

a.each do |number|
  puts n

The result in irb terminal or inside the browser:
=> [1,2,3,4]

Why this "=> [1,2,3,4]" appears inside the browser? I can't create a single list in my page because the whole structure appears...

Thank you! I didn't found nothing in the documentation about this.

Last Edited by josiasbh at 16 Jan 15:53

I'm not sure about in the browser, or even what you mean by that. As for irb, though, it's showing you the return value of the expression you typed. The return value of the .each method is the object it was called on. Similarly you may have noticed that if you just tell it to puts something, like "puts 1+1" or "puts 'Hello, world!'", you'll get "=> nil"... because puts returns nil.

This is very common in interactive interpretive environments, sometimes also called REPLs (Read/Eval/Print Loops). In languages where you can declare that something doesn't return any value, such things won't result in such a display. In Ruby, however, everything returns a value, even if that value is nil.

Forum Rating: 100
Sign In To Rate Post

Thank you! I get the point. Another problem in my code was this "=" operator in wrong place:

$= CODE $>
end %>

I saw that i should not use this "=" operator in this case because i won't return nothing, just print the elements inside a list.

Thank you again. I'm very happy learning Rails. It's hard to get all things but the time building an app is short.


Last Edited by josiasbh at 18 Jan 02:10
3 Posts
Login to add your message