Reputation: 620
I have some code:
# frozen_string_literal: true
require 'httpclient'
client = HTTPClient.new
response = client.get 'https://httpbin.org/get'
body = response.body
puts body
Why RubyMine have warning about Method invocation may produce 'NoMethodError'?
Upvotes: 1
Views: 3398
Reputation: 1581
There's a corresponding issue on the RubyMine's tracker so you can follow it: https://youtrack.jetbrains.com/issue/RUBY-24592
Upvotes: 2
Reputation: 33420
Basically, any object that doesn't respond to the method that's being invoked will raise a NoMethodError.
class Response
def body
'hardcoded body'
end
end
class ResponseWithoutBody; end
p Response.new.body
# "hardcoded body"
p ResponseWithoutBody.new.body
# `<main>': undefined method `body' for #<ResponseWithoutBody:0x00007fe903028e08> (NoMethodError)
In your case, if response
returns nil
, or any other object which doesn't implement body
, then you're going to have a NoMethodError
.
If you're pretty sure, you're never going to get nil
after invoking get
on client
, then you can omit that warning message.
Upvotes: 2