Run Ruby program until error - ruby

Is there a way to make a Ruby program keep executing until the program has an error? I want my loop to stop when the program returns an error.
Thanks

A infinite loop can help?
while true do
your code
end
If your code throw an error the loop stops.

This is another example. Will run infinite times till exception comes and also handles your exception and then exit form code.
inc = 5
while true do
begin
puts 4/inc
inc-=1
rescue Exception=> e
puts e
exit
end
end

Related

before:(all) execution in Rspec

How to forcefully stop the execution of before:(all) and the corresponding it blocks in a test framework developed using Rspec?
before:(all) do
#test = check()
if #test
end
end
The check function returns true or false. If false is returned, then the corresponding examples should not get executed. How to do this?
From your description it seems like you want to stop executing tests if the check is false:
before(:all) do
unless check
puts 'Check failed, stopping test suite...'
exit
end
end
exit initiates the termination of a Ruby script by raising the SystemExit exception

Ruby closing program window on Windows 7

I've download Ruby using rubyinstaller. Apps closed itself after finishing executing, so i put gets at the end of it - it works, but if there is an error it closes, and I have problem with debugging. How can i prevent this?
Probably the best way is to run your Ruby program from console (command line).
An alternative hack is to wrap your main program in an exception handler:
begin
... your code here ...
rescue => exc
puts "#{exc.class}: #{exc}" # write exception message
puts exc.backtrace.join "\n" # write backtrace
gets # wait for Return
end

Getting end of file reached (EOFError) using ruby webdriver

Some of my tests pass one time then fail with "end of file reached(EOFError)". Can't figure out what causes this consistency issue. Sometimes it fails when filling out a form. Other times it fails when clicking a button.
Using the following:
OSX 10.9.3
Watir-webdriver 0.6.10
Ruby 1.9.3
Chrome 35.0
Chromedriver ChromeDriver v2.10
Not sure what the issue is but a simple work around to get rid of this error is to use a Begin/Rescue statement around the code that is causing this error (check what line number the terminal output says is causing the error).
For example:
browser.close #This is the line giving the EOFError
Do the following:
begin
browser.close #if there is an error: jump to the rescue statement
rescue
#don't put any code in the rescue statement (ignore the error)
end
#rest of code
The way the begin/rescue statement works is if the code in the begin statement causes an error, it will run the code in the rescue statement. In this case, since there isn't any code in the rescue statement, it will merely ignore the error and continue with the rest of the code.

Equivalent to Perl's END block in Ruby

Is there a Perl equivalent END block in Ruby? In Perl, if I specify an END block, the code in that block will get executed no matter where the program bails out. It is great functionality for closing open file handles. Does Ruby support similar functionality? I tried Ruby's "END{}" block but that doesnt seem to get called if I had an exit in the code due to an error.
Thanks!
Use at_exit, which will run regardless of whether an exception was raised or not:
at_exit { puts 'exited!' }
raise
prints "exited" as expected.
You should only consider this if you cannot use an ensure, as at_exit causes logic to reside far away from where the actual exit occurs.
Yes. A block may have an 'ensure' clause. Here's an example:
begin
# This will cause a divide by zero exception
puts 3 / 0
rescue Exception => e
puts "An error occurred: #{e}"
ensure
puts "I get run anyway"
end
Running this produces:
An error occurred: divided by 0
I get run anyway

How to create an exit message

Is there a one line function call that quits the program and displays a message? I know in Perl it's as simple as:
die("Message goes here")
I'm tired of typing this:
puts "Message goes here"
exit
The abort function does this. For example:
abort("Message goes here")
Note: the abort message will be written to STDERR as opposed to puts which will write to STDOUT.
If you want to denote an actual error in your code, you could raise a RuntimeError exception:
raise RuntimeError, 'Message goes here'
This will print a stacktrace, the type of the exception being raised and the message that you provided. Depending on your users, a stacktrace might be too scary, and the actual message might get lost in the noise. On the other hand, if you die because of an actual error, a stacktrace will give you additional information for debugging.
I got here searching for a way to execute some code whenever the program ends.
Found this:
Kernel.at_exit { puts "sayonara" }
# do whatever
# [...]
# call #exit or #abort or just let the program end
# calling #exit! will skip the call
Called multiple times will register multiple handlers.
I've never heard of such a function, but it would be trivial enough to implement...
def die(msg)
puts msg
exit
end
Then, if this is defined in some .rb file that you include in all your scripts, you are golden.... just because it's not built in doesn't mean you can't do it yourself ;-)

Resources