0

I think it´s perhaps a simple question, but I don´t know what to do

def text1
  while $i < 10  
    $i = $i + 1 
    puts $i
    redirect :action => :index
  end
end

I want to show all values of the while loop. know i only see the start value 1 and the end value 10. How can i display 1 2 3...and 10.

And the next question, is it possibile to overwrite the value? because I need only one value changing every second.

Is there a possibility (i.e sleep 1 seconnd) to show all values?? please with the correct code thank you :)

taiansu
  • 2,735
  • 2
  • 22
  • 29

2 Answers2

2

The correct while loop syntax in Ruby would be

while $i < 10 do
    $i += 1
    puts $i
end

And you should redirect when you're done from the loop and not whilst in the loop. That's probably why you're not seeing the rest since your exiting the loop prematurely.

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
  • 2
    There's also no need to use a global variable here. I think that might be why OP was seeing strange results (because the global variable was set by the second time calling the action) – Ben Taitelbaum Jun 26 '13 at 09:22
-3

Your spacing isn't correct it should be

  def text1
    while $i < 10

      $i = $i + 1 
     puts $i
     redirect :action => :index
    end
  end

and for sleeping see->; sleep

Vin

Community
  • 1
  • 1
  • I have never seen a spacing like this... where did you get that? You should stick to this style guide https://github.com/styleguide/ruby – Simon Woker Jun 26 '13 at 09:34