In a Rails project, I have following error:
undefined local variable or method ` response' for #<Deliveries::CheckJobService:0x00007fce8548dd60> Did you mean? response
And here is the code:
delivery_status = response['status']
I don't see the error
I try new things, and the error is even weirder:
def call
return false if #order.stuart_job_id.nil?
response = stuart_check_job
if response.nil?
#order.update(delivery_status: 'sth went wrong')
else
delivery_status = 2
delivered_at = 4
#order.update(delivery_status: delivery_status, delivered_at: delivered_at)
end
return true
end
This is the error:
undefined local variable or method ` 2' for #<Deliveries::CheckJobService:0x00007fce8490fc40>
I don't see any single quotation
Related
I try to connect soap via savon, I get an error:
(a:InternalServiceFault) Object reference not set to an instance of an object.
Extracted source (around line #85):
def raise_soap_and_http_errors!
raise soap_fault if soap_fault?
raise http_error if http_error?
end
here is my code:
client = Savon.client(wsdl: 'http://moghim24.ir:8080/Moghim24Scripts/Moghim24Services.svc?wsdl')
response = client.call(:open_tempfllist) do
message fd: '96/01/01'.to_s, ld: '97/01/01'.to_s, cust: '1005'.to_s, pass: '233344'.to_s
end
render :json => response.body
the error happens at this raise soap_fault if soap_fault?
I have the following code (with a few debug lines added):
Ruby:
re_dict = {}
re_dict['state'] = 'pending' #set initial status to pending
puts re_dict, re_dict.class.to_s
puts re_dict['state'], re_dict['state'].class.to_s
puts re_dict['state'].casecmp('pending')
while re_dict['state'].casecmp('pending') == 0 do
stuff
end
Output
state: pending
state class: String
class compared to 'pending': 0
Completed 500 Internal Server Error in 66ms
NoMethodError (undefined method `casecmp' for nil:NilClass):
What is causing this? How am I losing the value of my hash?
This will happen only when you remove 'state' key from re_dict hash inside your while loop:
while re_dict['state'].casecmp('pending') == 0 do
puts re_dict
re_dict = {}
end
#=> {"state"=>"pending"}
#=> NoMethodError: undefined method `casecmp' for nil:NilClass
Since, key 'state' is not available anymore, calling re_dict['state'] will give nil, that's why you're getting undefined method casecmp' for nil:NilClass
I've got some very strange issue which I cannot debug.
This is my code:
def find_order_and_payment
#payment = Spree::Payment.find_by_identifier(params['session_id'])
Rails.logger.info "payment_id #{#payment.id}"
#order = #payment.order
end
And this is the output:
I, [2014-01-17T16:39:43.084827 #12342] INFO -- : payment_id 187
I, [2014-01-17T16:39:43.090718 #12332] INFO -- : Completed 500 Internal Server Error in 448ms
NoMethodError (undefined method `id' for nil:NilClass):
app/controllers/payu_status_controller.rb:36:in `find_order_and_payment'
Line 36 is the line with Rails.logger. I don't understand, why I get correct id, but same line returns undefined method id? If I'll call above code from console everything works as expected.
This is a common pattern: If a variable doesn't exist I get an undefined local variable or method error.
The existing code has if variable_name.present? but this didn't account for the variable not existing.
How can I check the value of the variable and also account for it not existing at all?
I've tried:
if (defined? mmm) then
if mmm.present? then
puts "true"
end
end
but Ruby still checks that inner mmm.present? and throws "no such variable" when it doesn't exist.
I'm sure there's a common pattern/solution to this.
Change the present? to != '' and use the && operator which only tries to evaluate the seond expression if the first one is true:
if defined?(mmm) && (mmm != '') then puts "yes" end
But actually as of 2019 this is no longer needed as both the below work
irb(main):001:0> if (defined? mm) then
irb(main):002:1* if mm.present? then
irb(main):003:2* p true
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> if (defined? mm) then
irb(main):007:1* p mm
irb(main):008:1> end
=> nil
On Ruby on Rails
if defined?(mm) && mm.present?
puts "acceptable variable"
end
On IRB
if defined?(mm) && !mm.blank? && !mm.nil?
puts "acceptable variable"
end
It can make sure you won't get undefined variable or nil or empty value.
Understand how defined? works
a = 1
defined?(a) # => "local-variable"
b = nil
defined?(b) # => "local-variable"
c = ""
defined?(c) # => "local-variable"
d = []
defined?(d) # => "local-variable"
$e = 'text'
defined?($e) # => "global-variable"
defined?(f) # => nil
defined?($g) # => nil
Note that defined? checks variable in the scope it is.
Why you need defined?
When there is possible of undefined variable presence, you cannot just check it with only .nil? for eaxample, you will have a chance to get NameError.
a = nil
a.nil? # => true
b.nil? # => NameError: undefined local variable or method `b'
I've a simple script that looks at Twitter username and gets me the location. But some of the username doesn't exist and I get error:
/usr/lib/ruby/1.8/open-uri.rb:277:in `open_http': 404 Not Found (OpenURI::HTTPError)
I've tried to rescue it, but I can't to make it work. Can anyone help? Thanks
a = []
my_file = File.new("location.txt", 'a+')
File.open('address.txt', 'r') do |f|
while line = f.gets
url = "http://twitter.com/#{line}"
doc = Nokogiri::HTML(open(url, 'User-Agent' => 'ruby'))
doc.css("#side #profile").each do |loc|
my_file.puts "http://twitter.com/#{line} #{loc.at_css(".adr").text}"
puts line
end
end
end
I also need help rescuing another error:
twitter.rb:14: undefined method `text' for nil:NilClass (NoMethodError)
Thanks.
Double quotes inside the other double quotes! Use single quotes for the call to at_css():
my_file.puts "http://twitter.com/#{line} #{loc.at_css('.adr').text}"
Turns out a simple rescue StandardError did the trick.