General Discussion - .each iteration - why am i getting double printings
.each iteration - why am i getting double printings
15:48 16-01-13
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.each do |number|
The result in irb terminal or inside the browser:
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
12:43 17-01-13
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.
02:06 18-01-13
Thank you! I get the point.
Another problem in my code was this "=" operator in wrong place:
$= CODE $>
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
General Discussion - .each iteration - why am i getting double printings
.each iteration - why am i getting double printings
15:48 16-01-13
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.each do |number|
The result in irb terminal or inside the browser:
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
12:43 17-01-13
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.
02:06 18-01-13
Thank you! I get the point.
Another problem in my code was this "=" operator in wrong place:
$= CODE $>
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