How to clearly compare old value with new one? - ruby

A common piece of code that I already repeated some times, and never liked it, consists in save a value, do an action, and evaluate the value afterwards. Look at the following example
old_files = project.files
project.some_operation_dealing_with_files
if old_files == project.files
puts "not changed"
else
puts "changed"
end
One of the problems on it is that on the first line, when you read old_files = project.files, it is not clear where you want to get. I imagine that if I could eliminate that variable, the code would be better, but I don't know how to achieve this. I'm also, of course, open to suggestions.
TLDR; Is there a way to rewrite the code without the old_file variable?

Make the object aware if the files state changes and provide a query method to test it. Set the did_change status to false at the start of each method that could change the files state.
project.some_operation_with_files
if project.files_did_change?
puts "Changed"
else
puts "No change."
end

Would returning a true/false status or throwing an exception work in your case? When the method returns false you should assume there were no changes in state of project anyway.
if project.some_operation_dealing_with_files
puts "Changed"
else
puts "Not Changed"
end
With an exception you can catch a specific type of error. Again if an error is thrown, project should be idempotent and the error should be handled gracefully.
begin
project.some_operation_dealing_with_files
puts "Changed"
rescue Errno::ENOENT
puts "Not Changed. File not found."
# Clean up and handle the error gracefully
end

Related

ruby - how can i still do something when there's error (example: NameError)

i manage to do "something"(like deleting files,etc) when exit or exit! is called from Object.
by changing the method exit and exit! inside Object class. (at_exit is too unreliable)
but then the "something" never execute if there's error such as NameError, etc.
is there way so i can make "something" that still execute if there's error.
(any error possible if necessary).
something like at_exit but works with all errors
thanks in advance for assistance. and forgive me if there's already question ask for this.
i do search a lot before asking here. and found nothing.
edit: i don't know where the original author of the code place the method. since the original author load it from dll files in the exe program the author used for launcher. (i can only edit After the original code take place...). so i think i need another approach for this... but i manage to make a workaround for my problem... by putting begin rescue in other places that send data to the original object. i filter the data send and throw my own error before it reach the main program... so i guess this works too.
Borrowing from an answer on a different thread, and definitely along the lines of what Marek commented, this is how you should handle errors in Ruby:
begin
# something which might raise an exception
rescue SomeExceptionClass => some_variable
# code that deals with some exception
rescue SomeOtherException => some_other_variable
# code that deals with some other exception
else
# code that runs only if *no* exception was raised
ensure
# ensure that this code always runs, no matter what
end
Original credit: Begin, Rescue and Ensure in Ruby?

How do I continue to validate next element in an array for a block after a value fails in the block

I am trying to validate strings in a array with in if else.
strings = ["Hello", "goodluck", "Bye" ]
strings.each do |i|
if i == "Hello"
#browser.text.should == ("Welcome")
elsif i == 'goodluck'
#browser.text.should == ("Good Bye")
end
next
end
So here for first condition, the browser doesnot have text "welcome", so it fails. But the iteration doesnot continue to validate next element in array. Please guide me if I am going wrong anywhere? or any other ways to do where all the elements of array is tested for the block irrespective of fail.
If you're inside a testcase, (assuming you're using rspec or something similar) a failed assertion will stop the whole testcase (because it's failed then). Technically a failed assertion raises an Exception, which get's caught somewhere outside the testcase (in rspec a 'it {}') to display the reason why the test failed.
In your case it might help to split up your test in to several cases, each with one assertion, but to give better advise I'd need to know what your trying to do.

Cucumber "puts" in After hook not outputting anything

In Cucumber, in my env.rb file, I have a before & after hook set up (well, a few of them, some linked to specific tags) but have found the after hooks don't output anything when I put a puts in them.
For example, this works:
Before do
puts "before the scenario"
end
but this doesn't:
After do
puts "after the scenario"
end
It seems that the after hooks do get run (as there's a particular line I'm having a problem with in the after hook & in trying to debug that, I've found this problem) but they're just not outputting anything.
All my searching has proved fruitless, can't find anyone else with similar problems. Can anyone tell if I'm doing something wrong?
Cucumber overrides the puts message in the RbWorld class so that anything written with puts gets properly broadcasted to all formatters. In the case of the pretty formatter, these go into a delayed_messages collection, until it calls print_messages, which it appears to do after each step name has been printed, presumably so that messages appear to be nested under the step in which they were generated.
For some reason there is no final call to print_messages in the pretty formatter, I'm not sure if it's an omission or deliberate since it would look less 'pretty' to have spurious messages in the output.
Interestingly if you add a second scenario, you'll see 'after the scenario' printed as the first message when the second scenario gets run, that's the delayed_messages collection in action.
In summary, you're doing nothing wrong, it's just how Cucumber hijacks the puts method. If you're not too bothered about these messages being nicely formatted, then you can just replace puts with STDOUT.puts to bypass Cucumber's formatting.
Before do
p "Go!"
puts "Go!"
end
After do
p "Stop!"
puts "Stop!"
$stdout.puts "Stop!"
end
output of this snippet may help to understand why 'puts' not working in After hook.

Ruby Exceptions in a loop

I have a ruby script that loops through a list of shortened urls (around 2,000 - 3,000 at a time). At the moment everything is peachy until a hit a url that is malformed, timedout etc. When an error occurs my script dies. How can I setup my loop to skip to the next record when/if such an error occurs.
my loop looks like this:
blah.foo do |barurl|
mymethod(barurl)
my mymethod looks like this:
def mymethod(barurl)
begin
stuff
...
return expandedurl
rescue
return "Problem expanding link"
end
end
Should my begin/end logic be wrapped around my loop instead of the method?
Because you need to skip the malformed url, you should use the exception message to control the loop
blah.foo do |barurl|
begin
mymethod(barurl)
rescue YourTypeOfException
next
end
end
and inside the method raise the exception
def mymethod(barurl)
stuff
...
raise YourTypeOfException, "this url is not valid"
...
end
I found the existing answers unsatisfying, and reading the documentation suggests to me that the OP had something more like the example suggested there in mind:
[0, 1, 2].map do |i|
10 / i
rescue ZeroDivisionError
nil
end
#=> [nil, 10, 5]
The docs specifically note that a rescue block permits the loop to continue on a caught exception (as indicated by the example).
Yes. All your method does is consume the exception and return another arbitrary object in order to indicate an error.
Your method shouldn't handle its own exceptional conditions. It is just rude on its part to make assumptions about how the caller will react.
blah.foo do |url|
begin
my_method url
rescue
next
end
end
Whether to skip to the next URL or print a message is not a decision the method should be making. Its only concern should be working with the URL.
With that said, you should simply let it propagate and only rescue from it when you can actually deal with it. Don't rescue from a TimeoutError if all you can do is return :timeout.
Do rescue when you need to clean up resources or simply let the user know an error occurred.
Also, rescuing from every possible error just to make them go away is a nice way to introduce bugs. Be as specific as possible.
having exception handling within your method is proper way of doing it, so your implementation is fine
i can only point some ruby sytax sugar to you:
def some_method
# here goes the code
rescue Exception => e
# here goes specific exception/error handling
rescue
# here goes error handling (not Exception handling though!)
else
# do this block when no exceptions are raised
ensure
# do this every time
end
btw you don't need return statements, last value of code block is always returned implicitly
ah i guess i misread your question in the "how to skip next record"
if you want to skip the record after current one that was incorrect you would have to return error code from your parsing method and set up skipping within your loop using break or next keywords
It should be inside the loop, so the loop structure isn't exited on an exception. But it looks like it already is--if you're rescuing inside the method that causes the exception, the loop should already continue normally, because it shouldn't be seeing the exception.

Get caller's caller for Ruby exception

Right now our application is running on Heroku, and in order to save restarts and possible latency the application is designed to cache all errors and send them to stderr and just server a stale cache until the issue is resolved by another push. This prevents application takedown...
However, the logs keep filling up excessively with entire traces which we don't want, so I built a little snippet (that is partially stolen from Rails) to parse caller and only send that line, however with errors being sent from a rescue, we get the wrong line so I was wondering how I could get the caller or the caller or if there was a better way to handle this situation. Here is the snippet:
module StdErr
def error(message)
file = 'Unknown'
line = '0'
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ caller[1]
file = $1
line = $2
end
$stderr.puts "ERROR: #{message} on line #{line} of #{file}"
end
end
If I get it, you want to raise a exception with a backtrace that begins on the caller of the caller. Do it:
def raise_cc(*args)
raise(*args)
rescue Exception
# Raises the same exception, removing the last 2 items
# from backtrace (this method and its caller). The first
# will be the caller of the caller of this method.
raise($!.class, $!.message, $!.backtrace[2..(-1)])
end
caller returns an array and you can go deeper into the call stack by looking at it's later elements (note that you're looking at caller[1])

Resources