Creating an Infinite Loop












16















I'm trying to create an infinite loop, where a block of code will be executed forever.



All loop documentation i have found warns against creating an infinite loop, but no examples of a working one.



If I have a block of code:



{ puts "foo"  
puts "bar"
sleep 300 }


How would I go about running this block forever?










share|improve this question




















  • 6





    You've tagged this ruby-on-rails. If you're trying to create an infinite loop in Rails, you're probably doing something horribly wrong. You can't do that in the same process that is serving your site, or the single thread of execution cannot actually respond to incoming requests. If you want to do something every 300 seconds, you need an asynchronous background job. You should describe your actual problem so we can provide you with real advice - using an infinite loop is a solution, not a problem.

    – meagar
    Nov 26 '14 at 1:49








  • 1





    Thank you for clarifying. This loop is not being used for rails. I have removed the tag. Thanks for the explanation!

    – Andrew Walz
    Nov 26 '14 at 2:18
















16















I'm trying to create an infinite loop, where a block of code will be executed forever.



All loop documentation i have found warns against creating an infinite loop, but no examples of a working one.



If I have a block of code:



{ puts "foo"  
puts "bar"
sleep 300 }


How would I go about running this block forever?










share|improve this question




















  • 6





    You've tagged this ruby-on-rails. If you're trying to create an infinite loop in Rails, you're probably doing something horribly wrong. You can't do that in the same process that is serving your site, or the single thread of execution cannot actually respond to incoming requests. If you want to do something every 300 seconds, you need an asynchronous background job. You should describe your actual problem so we can provide you with real advice - using an infinite loop is a solution, not a problem.

    – meagar
    Nov 26 '14 at 1:49








  • 1





    Thank you for clarifying. This loop is not being used for rails. I have removed the tag. Thanks for the explanation!

    – Andrew Walz
    Nov 26 '14 at 2:18














16












16








16


0






I'm trying to create an infinite loop, where a block of code will be executed forever.



All loop documentation i have found warns against creating an infinite loop, but no examples of a working one.



If I have a block of code:



{ puts "foo"  
puts "bar"
sleep 300 }


How would I go about running this block forever?










share|improve this question
















I'm trying to create an infinite loop, where a block of code will be executed forever.



All loop documentation i have found warns against creating an infinite loop, but no examples of a working one.



If I have a block of code:



{ puts "foo"  
puts "bar"
sleep 300 }


How would I go about running this block forever?







ruby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '14 at 7:28









vgoff

8,20122848




8,20122848










asked Nov 26 '14 at 1:39









Andrew WalzAndrew Walz

3972318




3972318








  • 6





    You've tagged this ruby-on-rails. If you're trying to create an infinite loop in Rails, you're probably doing something horribly wrong. You can't do that in the same process that is serving your site, or the single thread of execution cannot actually respond to incoming requests. If you want to do something every 300 seconds, you need an asynchronous background job. You should describe your actual problem so we can provide you with real advice - using an infinite loop is a solution, not a problem.

    – meagar
    Nov 26 '14 at 1:49








  • 1





    Thank you for clarifying. This loop is not being used for rails. I have removed the tag. Thanks for the explanation!

    – Andrew Walz
    Nov 26 '14 at 2:18














  • 6





    You've tagged this ruby-on-rails. If you're trying to create an infinite loop in Rails, you're probably doing something horribly wrong. You can't do that in the same process that is serving your site, or the single thread of execution cannot actually respond to incoming requests. If you want to do something every 300 seconds, you need an asynchronous background job. You should describe your actual problem so we can provide you with real advice - using an infinite loop is a solution, not a problem.

    – meagar
    Nov 26 '14 at 1:49








  • 1





    Thank you for clarifying. This loop is not being used for rails. I have removed the tag. Thanks for the explanation!

    – Andrew Walz
    Nov 26 '14 at 2:18








6




6





You've tagged this ruby-on-rails. If you're trying to create an infinite loop in Rails, you're probably doing something horribly wrong. You can't do that in the same process that is serving your site, or the single thread of execution cannot actually respond to incoming requests. If you want to do something every 300 seconds, you need an asynchronous background job. You should describe your actual problem so we can provide you with real advice - using an infinite loop is a solution, not a problem.

– meagar
Nov 26 '14 at 1:49







You've tagged this ruby-on-rails. If you're trying to create an infinite loop in Rails, you're probably doing something horribly wrong. You can't do that in the same process that is serving your site, or the single thread of execution cannot actually respond to incoming requests. If you want to do something every 300 seconds, you need an asynchronous background job. You should describe your actual problem so we can provide you with real advice - using an infinite loop is a solution, not a problem.

– meagar
Nov 26 '14 at 1:49






1




1





Thank you for clarifying. This loop is not being used for rails. I have removed the tag. Thanks for the explanation!

– Andrew Walz
Nov 26 '14 at 2:18





Thank you for clarifying. This loop is not being used for rails. I have removed the tag. Thanks for the explanation!

– Andrew Walz
Nov 26 '14 at 2:18












4 Answers
4






active

oldest

votes


















25














loop do
puts 'foo'
puts 'bar'
sleep 300
end





share|improve this answer
























  • Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :)

    – Amadan
    Nov 26 '14 at 2:10



















14














Here are some examples of infinite loops using blocks.



Loop



loop do
puts "foo"
puts "bar"
sleep 300
end


While



while true
puts "foo"
puts "bar"
sleep 300
end


Until



until false
puts "foo"
puts "bar"
sleep 300
end


Lambda



-> { puts "foo" ; puts "bar" ; sleep 300}.call until false


There are a few variations of the lambda as well, using the non-stabby lambda syntax. Also we could use a Proc.



Begin..End



begin
puts "foo"
puts "bar"
sleep 300
end while false





share|improve this answer

































    0














    I have tried all but with inputs loop only worked as infinty loop till I get a valid input:



    loop do
    a = gets.to_i
    if (a >= 2)
    break
    else
    puts "Invalid Input, Please enter a correct Value >=2: "
    end
    end





    share|improve this answer































      -3














      1) While loop:



      While 1==1 # As the condition 1 is equal to 1 is true, it always runs.
      puts "foo"
      puts "bar"
      sleep 300
      end


      2) Recursion :



          def infiniteLoop # Using recursion concept
      puts "foo"
      puts "bar"
      sleep 300
      infiniteLoop #Calling this method again
      end


      EDIT : I thought this would work, but as Gabriel mentioned, we would get SystemStackError.



      3) Loop



         loop do
      puts "foo"
      ....
      end


      4) Using unless



      unless 1 == 2  # Unless 1 is equal to 2 , it keeps running
      puts "foo"
      ...
      end





      share|improve this answer





















      • 4





        unless is not a loop construct, it is a synonym for if not. You might have been thinking of until, a synonym for while not. And while we're on while, Ruby is case-sensitive: While is an undefined constant, and not a keyword like while is; and as a keyword, it does not accept a block, so while ... do is a syntax error as well.

        – Amadan
        Nov 26 '14 at 2:08








      • 2





        The recursion alternative will not work as with each iteration you're adding a level to the stack and eventually a SystemStackError will be raised.

        – Gabriel de Oliveira
        Nov 26 '14 at 2:25











      • Thank you guys, about corrections.

        – Rahul Dess
        Nov 26 '14 at 2:43











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27139937%2fcreating-an-infinite-loop%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      25














      loop do
      puts 'foo'
      puts 'bar'
      sleep 300
      end





      share|improve this answer
























      • Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :)

        – Amadan
        Nov 26 '14 at 2:10
















      25














      loop do
      puts 'foo'
      puts 'bar'
      sleep 300
      end





      share|improve this answer
























      • Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :)

        – Amadan
        Nov 26 '14 at 2:10














      25












      25








      25







      loop do
      puts 'foo'
      puts 'bar'
      sleep 300
      end





      share|improve this answer













      loop do
      puts 'foo'
      puts 'bar'
      sleep 300
      end






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 26 '14 at 1:41









      Todd A. JacobsTodd A. Jacobs

      56.2k1192159




      56.2k1192159













      • Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :)

        – Amadan
        Nov 26 '14 at 2:10



















      • Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :)

        – Amadan
        Nov 26 '14 at 2:10

















      Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :)

      – Amadan
      Nov 26 '14 at 2:10





      Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :)

      – Amadan
      Nov 26 '14 at 2:10













      14














      Here are some examples of infinite loops using blocks.



      Loop



      loop do
      puts "foo"
      puts "bar"
      sleep 300
      end


      While



      while true
      puts "foo"
      puts "bar"
      sleep 300
      end


      Until



      until false
      puts "foo"
      puts "bar"
      sleep 300
      end


      Lambda



      -> { puts "foo" ; puts "bar" ; sleep 300}.call until false


      There are a few variations of the lambda as well, using the non-stabby lambda syntax. Also we could use a Proc.



      Begin..End



      begin
      puts "foo"
      puts "bar"
      sleep 300
      end while false





      share|improve this answer






























        14














        Here are some examples of infinite loops using blocks.



        Loop



        loop do
        puts "foo"
        puts "bar"
        sleep 300
        end


        While



        while true
        puts "foo"
        puts "bar"
        sleep 300
        end


        Until



        until false
        puts "foo"
        puts "bar"
        sleep 300
        end


        Lambda



        -> { puts "foo" ; puts "bar" ; sleep 300}.call until false


        There are a few variations of the lambda as well, using the non-stabby lambda syntax. Also we could use a Proc.



        Begin..End



        begin
        puts "foo"
        puts "bar"
        sleep 300
        end while false





        share|improve this answer




























          14












          14








          14







          Here are some examples of infinite loops using blocks.



          Loop



          loop do
          puts "foo"
          puts "bar"
          sleep 300
          end


          While



          while true
          puts "foo"
          puts "bar"
          sleep 300
          end


          Until



          until false
          puts "foo"
          puts "bar"
          sleep 300
          end


          Lambda



          -> { puts "foo" ; puts "bar" ; sleep 300}.call until false


          There are a few variations of the lambda as well, using the non-stabby lambda syntax. Also we could use a Proc.



          Begin..End



          begin
          puts "foo"
          puts "bar"
          sleep 300
          end while false





          share|improve this answer















          Here are some examples of infinite loops using blocks.



          Loop



          loop do
          puts "foo"
          puts "bar"
          sleep 300
          end


          While



          while true
          puts "foo"
          puts "bar"
          sleep 300
          end


          Until



          until false
          puts "foo"
          puts "bar"
          sleep 300
          end


          Lambda



          -> { puts "foo" ; puts "bar" ; sleep 300}.call until false


          There are a few variations of the lambda as well, using the non-stabby lambda syntax. Also we could use a Proc.



          Begin..End



          begin
          puts "foo"
          puts "bar"
          sleep 300
          end while false






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 17 '17 at 0:47

























          answered Nov 26 '14 at 6:54









          vgoffvgoff

          8,20122848




          8,20122848























              0














              I have tried all but with inputs loop only worked as infinty loop till I get a valid input:



              loop do
              a = gets.to_i
              if (a >= 2)
              break
              else
              puts "Invalid Input, Please enter a correct Value >=2: "
              end
              end





              share|improve this answer




























                0














                I have tried all but with inputs loop only worked as infinty loop till I get a valid input:



                loop do
                a = gets.to_i
                if (a >= 2)
                break
                else
                puts "Invalid Input, Please enter a correct Value >=2: "
                end
                end





                share|improve this answer


























                  0












                  0








                  0







                  I have tried all but with inputs loop only worked as infinty loop till I get a valid input:



                  loop do
                  a = gets.to_i
                  if (a >= 2)
                  break
                  else
                  puts "Invalid Input, Please enter a correct Value >=2: "
                  end
                  end





                  share|improve this answer













                  I have tried all but with inputs loop only worked as infinty loop till I get a valid input:



                  loop do
                  a = gets.to_i
                  if (a >= 2)
                  break
                  else
                  puts "Invalid Input, Please enter a correct Value >=2: "
                  end
                  end






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 1:17









                  Abdelrahman FaragAbdelrahman Farag

                  30123




                  30123























                      -3














                      1) While loop:



                      While 1==1 # As the condition 1 is equal to 1 is true, it always runs.
                      puts "foo"
                      puts "bar"
                      sleep 300
                      end


                      2) Recursion :



                          def infiniteLoop # Using recursion concept
                      puts "foo"
                      puts "bar"
                      sleep 300
                      infiniteLoop #Calling this method again
                      end


                      EDIT : I thought this would work, but as Gabriel mentioned, we would get SystemStackError.



                      3) Loop



                         loop do
                      puts "foo"
                      ....
                      end


                      4) Using unless



                      unless 1 == 2  # Unless 1 is equal to 2 , it keeps running
                      puts "foo"
                      ...
                      end





                      share|improve this answer





















                      • 4





                        unless is not a loop construct, it is a synonym for if not. You might have been thinking of until, a synonym for while not. And while we're on while, Ruby is case-sensitive: While is an undefined constant, and not a keyword like while is; and as a keyword, it does not accept a block, so while ... do is a syntax error as well.

                        – Amadan
                        Nov 26 '14 at 2:08








                      • 2





                        The recursion alternative will not work as with each iteration you're adding a level to the stack and eventually a SystemStackError will be raised.

                        – Gabriel de Oliveira
                        Nov 26 '14 at 2:25











                      • Thank you guys, about corrections.

                        – Rahul Dess
                        Nov 26 '14 at 2:43
















                      -3














                      1) While loop:



                      While 1==1 # As the condition 1 is equal to 1 is true, it always runs.
                      puts "foo"
                      puts "bar"
                      sleep 300
                      end


                      2) Recursion :



                          def infiniteLoop # Using recursion concept
                      puts "foo"
                      puts "bar"
                      sleep 300
                      infiniteLoop #Calling this method again
                      end


                      EDIT : I thought this would work, but as Gabriel mentioned, we would get SystemStackError.



                      3) Loop



                         loop do
                      puts "foo"
                      ....
                      end


                      4) Using unless



                      unless 1 == 2  # Unless 1 is equal to 2 , it keeps running
                      puts "foo"
                      ...
                      end





                      share|improve this answer





















                      • 4





                        unless is not a loop construct, it is a synonym for if not. You might have been thinking of until, a synonym for while not. And while we're on while, Ruby is case-sensitive: While is an undefined constant, and not a keyword like while is; and as a keyword, it does not accept a block, so while ... do is a syntax error as well.

                        – Amadan
                        Nov 26 '14 at 2:08








                      • 2





                        The recursion alternative will not work as with each iteration you're adding a level to the stack and eventually a SystemStackError will be raised.

                        – Gabriel de Oliveira
                        Nov 26 '14 at 2:25











                      • Thank you guys, about corrections.

                        – Rahul Dess
                        Nov 26 '14 at 2:43














                      -3












                      -3








                      -3







                      1) While loop:



                      While 1==1 # As the condition 1 is equal to 1 is true, it always runs.
                      puts "foo"
                      puts "bar"
                      sleep 300
                      end


                      2) Recursion :



                          def infiniteLoop # Using recursion concept
                      puts "foo"
                      puts "bar"
                      sleep 300
                      infiniteLoop #Calling this method again
                      end


                      EDIT : I thought this would work, but as Gabriel mentioned, we would get SystemStackError.



                      3) Loop



                         loop do
                      puts "foo"
                      ....
                      end


                      4) Using unless



                      unless 1 == 2  # Unless 1 is equal to 2 , it keeps running
                      puts "foo"
                      ...
                      end





                      share|improve this answer















                      1) While loop:



                      While 1==1 # As the condition 1 is equal to 1 is true, it always runs.
                      puts "foo"
                      puts "bar"
                      sleep 300
                      end


                      2) Recursion :



                          def infiniteLoop # Using recursion concept
                      puts "foo"
                      puts "bar"
                      sleep 300
                      infiniteLoop #Calling this method again
                      end


                      EDIT : I thought this would work, but as Gabriel mentioned, we would get SystemStackError.



                      3) Loop



                         loop do
                      puts "foo"
                      ....
                      end


                      4) Using unless



                      unless 1 == 2  # Unless 1 is equal to 2 , it keeps running
                      puts "foo"
                      ...
                      end






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 26 '14 at 2:42

























                      answered Nov 26 '14 at 1:55









                      Rahul DessRahul Dess

                      7831930




                      7831930








                      • 4





                        unless is not a loop construct, it is a synonym for if not. You might have been thinking of until, a synonym for while not. And while we're on while, Ruby is case-sensitive: While is an undefined constant, and not a keyword like while is; and as a keyword, it does not accept a block, so while ... do is a syntax error as well.

                        – Amadan
                        Nov 26 '14 at 2:08








                      • 2





                        The recursion alternative will not work as with each iteration you're adding a level to the stack and eventually a SystemStackError will be raised.

                        – Gabriel de Oliveira
                        Nov 26 '14 at 2:25











                      • Thank you guys, about corrections.

                        – Rahul Dess
                        Nov 26 '14 at 2:43














                      • 4





                        unless is not a loop construct, it is a synonym for if not. You might have been thinking of until, a synonym for while not. And while we're on while, Ruby is case-sensitive: While is an undefined constant, and not a keyword like while is; and as a keyword, it does not accept a block, so while ... do is a syntax error as well.

                        – Amadan
                        Nov 26 '14 at 2:08








                      • 2





                        The recursion alternative will not work as with each iteration you're adding a level to the stack and eventually a SystemStackError will be raised.

                        – Gabriel de Oliveira
                        Nov 26 '14 at 2:25











                      • Thank you guys, about corrections.

                        – Rahul Dess
                        Nov 26 '14 at 2:43








                      4




                      4





                      unless is not a loop construct, it is a synonym for if not. You might have been thinking of until, a synonym for while not. And while we're on while, Ruby is case-sensitive: While is an undefined constant, and not a keyword like while is; and as a keyword, it does not accept a block, so while ... do is a syntax error as well.

                      – Amadan
                      Nov 26 '14 at 2:08







                      unless is not a loop construct, it is a synonym for if not. You might have been thinking of until, a synonym for while not. And while we're on while, Ruby is case-sensitive: While is an undefined constant, and not a keyword like while is; and as a keyword, it does not accept a block, so while ... do is a syntax error as well.

                      – Amadan
                      Nov 26 '14 at 2:08






                      2




                      2





                      The recursion alternative will not work as with each iteration you're adding a level to the stack and eventually a SystemStackError will be raised.

                      – Gabriel de Oliveira
                      Nov 26 '14 at 2:25





                      The recursion alternative will not work as with each iteration you're adding a level to the stack and eventually a SystemStackError will be raised.

                      – Gabriel de Oliveira
                      Nov 26 '14 at 2:25













                      Thank you guys, about corrections.

                      – Rahul Dess
                      Nov 26 '14 at 2:43





                      Thank you guys, about corrections.

                      – Rahul Dess
                      Nov 26 '14 at 2:43


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27139937%2fcreating-an-infinite-loop%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas