Started learning Ruby a week ago. The most frustrating part so far has been exception handling. Been looking for an example for what I want to do for hours.
I am looping through an array of API codes, grabbing text via Net::Http. Sometimes the grab returns nil or empty, and I'm trying to test for it in a way that will let me retry the grab on that exception until it works.
I am fairly certain I need to do something like
array.each do |api_key|
begin
result = # the code to grab the page via the API key
if result.empty or result.nil
raise SomeKindOfExceptionThing
end
rescue SomeKindOfExceptionThing
puts "some error message"
retry
else
# Act on the valid return for result
end
end
I have no idea how to properly form this so it does what I want. Most of the documentation I've found on exceptions is for handling predefined types of errors or just a generic rescue.
nil?andempty?notnilandempty.