Creating an Infinite Loop
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
add a comment |
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
6
You've tagged thisruby-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
add a comment |
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
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
ruby
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 thisruby-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
add a comment |
6
You've tagged thisruby-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
add a comment |
4 Answers
4
active
oldest
votes
loop do
puts 'foo'
puts 'bar'
sleep 300
end
Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :)
– Amadan
Nov 26 '14 at 2:10
add a comment |
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
add a comment |
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
add a comment |
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
4
unless
is not a loop construct, it is a synonym forif not
. You might have been thinking ofuntil
, a synonym forwhile not
. And while we're onwhile
, Ruby is case-sensitive:While
is an undefined constant, and not a keyword likewhile
is; and as a keyword, it does not accept a block, sowhile ... 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 aSystemStackError
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
loop do
puts 'foo'
puts 'bar'
sleep 300
end
Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :)
– Amadan
Nov 26 '14 at 2:10
add a comment |
loop do
puts 'foo'
puts 'bar'
sleep 300
end
Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :)
– Amadan
Nov 26 '14 at 2:10
add a comment |
loop do
puts 'foo'
puts 'bar'
sleep 300
end
loop do
puts 'foo'
puts 'bar'
sleep 300
end
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited May 17 '17 at 0:47
answered Nov 26 '14 at 6:54
vgoffvgoff
8,20122848
8,20122848
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 1 at 1:17
Abdelrahman FaragAbdelrahman Farag
30123
30123
add a comment |
add a comment |
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
4
unless
is not a loop construct, it is a synonym forif not
. You might have been thinking ofuntil
, a synonym forwhile not
. And while we're onwhile
, Ruby is case-sensitive:While
is an undefined constant, and not a keyword likewhile
is; and as a keyword, it does not accept a block, sowhile ... 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 aSystemStackError
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
add a comment |
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
4
unless
is not a loop construct, it is a synonym forif not
. You might have been thinking ofuntil
, a synonym forwhile not
. And while we're onwhile
, Ruby is case-sensitive:While
is an undefined constant, and not a keyword likewhile
is; and as a keyword, it does not accept a block, sowhile ... 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 aSystemStackError
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
add a comment |
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
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
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 forif not
. You might have been thinking ofuntil
, a synonym forwhile not
. And while we're onwhile
, Ruby is case-sensitive:While
is an undefined constant, and not a keyword likewhile
is; and as a keyword, it does not accept a block, sowhile ... 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 aSystemStackError
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
add a comment |
4
unless
is not a loop construct, it is a synonym forif not
. You might have been thinking ofuntil
, a synonym forwhile not
. And while we're onwhile
, Ruby is case-sensitive:While
is an undefined constant, and not a keyword likewhile
is; and as a keyword, it does not accept a block, sowhile ... 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 aSystemStackError
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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