1

I learning ruby and playing it with restsclient I have following test the code and I'm expecting that to return 1/false. I can't seem to make it work.

n@lap-jta102:~/tsamcode$ ./get.rb
n@lap-jta102:~/tsamcode$ echo $?
0 

#!/usr/bin/env ruby

require 'rest_client'
require 'json'

begin
response = RestClient.get("https://admin:[email protected]/isam/host_records/187.0.0.1/hostnames", :content_type => :json, :accept => :json)
return true if response.code == 200
rescue => e
return false unless response != 200
end

1 Answer 1

1

$? is not set by return, but by exit. In fact, your return doesn't even do what you think. Try just this:

# one-returner.rb
return 1

$ ruby one-returner.rb
one-returner.rb:1:in `<main>': unexpected return (LocalJumpError)

The reason you're not getting an error in your program is the fact that you blanket-rescue this error when raised by return true (since you have an unrestricted rescue, which is a bad practice for exactly this reason, it can catch a wrong thing and leave you puzzled), and return false never executes (and thus never raises an error) due to unless.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. I found out about exit & exit!. I modified the it now the code that it after the rescue it will exit! and seems doing that I expected, but I guess not the way to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.