Equivalent of "Continue" in Ruby

Equivalent of continue in Ruby

Yes, it's called next.

for i in 0..5
if i < 2
next
end
puts "Value of local variable is #{i}"
end

This outputs the following:

Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
=> 0..5

In Ruby, how do I skip a loop in a .each loop, similar to 'continue'

Use next:

(1..10).each do |a|
next if a.even?
puts a
end

prints:

1
3
5
7
9

For additional coolness check out also redo and retry.

Works also for friends like times, upto, downto, each_with_index, select, map and other iterators (and more generally blocks).

For more info see http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UL.

ruby: continue in same loop iteration after an exception

You're defining a begin-end block with a rescue clause. If any exception is raised by the block and there's a matching rescue clause, the block will stop executing and the rescue clause will be executed. If there's no matching rescue block, the error will bubble up (and hopefully be handled by another block, otherwise it'll be unhandled and your script will stop!) If there's an ensure clause, that'll get run too even if there's an exception.

So where does that leave us? If you want to defend against failure of individual steps and continue regardless, each step needs its own block:

3.times do |i|
begin
first_thing
rescue => e
puts "The first thing blew up! #{e.inspect}"
puts "I'll carry on anyway ¯\\_(ツ)_/¯"
end

begin
second_thing
third_thing
rescue => e
puts "Either the second or third thing blew up... #{e.inspect}"
puts "Straight on to the fourth thing!"
end

begin
fourth_thing
rescue => e
puts "Fourth thing blew up! #{e.inspect}"
end
end

It's a bit unusual to have a block like this which should carry on executing if something goes wrong - that's usually a good way to make one of the next steps go wrong too! You might need to make sure that you still have data integrity at each point, and that the later steps don't depend on something which might have failed to happen in the earlier steps.

Ruby: Continue a loop after catching an exception

In Ruby, continue is spelt next.

Continue script after rescue in ruby

Try this:

def inc
page3 = `curl https://api.statuspage.io/v1/pages/incidents.json?page=3 -H "Authorization: OAuth a8ef42"`
JSON.parse(page3).each do |h|
begin
puts "ID : #{h["id"]} , CREATED AT : #{h["created_at"]} , LINK : #{h["shortlink"]} , ISSUE NAME : #{h["name"]} , DESCRIPTION #{h["incident_updates"][0]["status"]} , DESCRIPTION #{h["incident_updates"][1]["status"]}"
rescue NoMethodError => e
puts e
end
end
end

Explanation:

Whenever the exception is caught it tries to exit out of the block in which the exception has occurred.

In your previous code, you're handling it in the scope of the function. So, when the exception was occurring in the iteration it was exiting out of the loop because because it wasn't handled inside the scope in which it was occurring (loop) and was caught right outside the loop because you wrote it there (outside the loop).

To continue the iteration process you must handle it where it was occurring so that the system must know that it's been handled perfectly and it can perform the next iteration.

How to code press key to continue

You can use STDIN from the IO class, rather than gets.

require 'io/console'                                                                                                       
def continue_story
print "press any key"
STDIN.getch
print " \r" # extra space to overwrite in case next sentence is short
end

puts "An awesome story begins..."
continue_story
puts "And ends after 2 lines"

This has the added bonus that it only requires one character to be entered (getch - get character) allowing the 'press any key' to work without a return or enter.

Alternative for the continue statement

The first continue; can be replaced by inverting the condition and putting the rest code inside if statement.

The second continue; can be simply removed because there are no code to execute after that.

bool neighCheck (int i, int j, int a[][COLLENGTH])
{
bool neighbourOnFire;
int x, y, neighX, neighY, curreNeigh;

/* Bool set up to change after neighbours looped*/
neighbourOnFire = false;
/* The neighbours -looping from -1 -> 1 to get index of each neighbour*/
for (x = -1; x < 2; x++) {
for (y = -1; y < 2; y++) {
/* Disregards current (middle) cell*/
if (!((x == 0) && (y == 0))) {
/* Get indexes of the neighbour we're looking at */
neighX = i + x;
neighY = j + y;
/* Checks for edges*/
if (neighX >= 0 && neighY >= 0 && neighX < ROWLENGTH
&& neighY < COLLENGTH) {
/* Get the neighbour using the indexes above */
curreNeigh = a[neighX][neighY];
/* Test to see if the neighbour is burning */
if (curreNeigh == fire) {
neighbourOnFire = true;
}
}
}
}
}
return neighbourOnFire;
}


Related Topics



Leave a reply



Submit