Error fetching status: undefined method `text' for nil:NilClass - ruby

I Don't know anything about Ruby, found code below which reports AWS status
https://gist.github.com/ktheory/1604786
/1.rb https://status.aws.amazon.com/rss/a4b-us-east-1.rss
Error fetching status: undefined method `text' for nil:NilClass
ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
latest_status = xml_doc.css("item title").first.text
print lastest_status
in `<main>': undefined method `text' for nil:NilClass (NoMethodError)

If first comes up empty and returns nil you can't just blunder along or your code will crash. You need to tread carefully:
latest_status = xml_doc.css("item title").first&.text
Or, if you're using an older version of Ruby and have ActiveSupport from Rails:
latest_status = xml_doc.css("item title").first.try(:text)
Or else you're going to need to do it the hard way:
latest_status = xml_doc.css("item title").first
latest_status &&= latest_status.text
You should probably figure out why that selector isn't working as it might not be correct and ends up returning nothing.

Related

undefined method `executeScript' for #<Selenium::WebDriver::Chrome::Bridge:0x007ffd0fa16e90>

undefined method executeScript' for <Selenium::WebDriver::Chrome::Bridge:0x007ffd0fa16e90> Did you mean? execute_script (NoMethodError)
I'm getting this error on any line with element.fire_event('onClick')
Chrome version 53.0.2785.143 (64-bit)
ChromeDriver 2.24.417412
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin16]
I'm using selenium-webdriver/page object.. I defined my element..
checkbox(:check_the_box, :id => 'checkboxid')
then tried to execute a fire_event on it..
check_the_box_element.fire_event('onClick')
then i receive the error above.
undefined method `executeScript' for # Did you mean? execute_script (NoMethodError)
Exception clearly states, It should be WebDriver#execute_script(script, *args) instead

What is the purpose of []= method on nil [duplicate]

This question already has an answer here:
In Ruby, why does nil[1]=1 evaluate to nil?
(1 answer)
Closed 6 years ago.
Since ruby 2.3.0, you can call []= method on nil. I don't understand the purpose of this method.
For instance:
nil[1] = 1
# or
nil['foo'] = 'bar'
but [] method does not exist:
nil[1]
# => NoMethodError: undefined method `[]' for nil:NilClass
The ruby 2.3.0 changelog does not mention that changes, although it seems close to the safe navigation operator.
What is the purpose of this operator?
That seems to be actually a bug in 2.3.0 - https://bugs.ruby-lang.org/issues/11976
It does not evaluate the arguments:
nil[undefined_index_variable] = raise "Fooo!" # => nil
That method isn't documented in Ruby 2.3.0 and I cannot reproduce this behavior in Ruby 2.3.1 (both examples raise NoMethodError: undefined method '[]=' for nil:NilClass).
Furthermore I reinstalled 2.3.0 and was only partly able to reproduce your examples:
$ rbenv install 2.3.0
Downloading ruby-2.3.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.bz2
Installing ruby-2.3.0...
Installed ruby-2.3.0 to /Users/spickermann/.rbenv/versions/2.3.0
$ rbenv shell 2.3.0
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
$ irb
irb > RUBY_VERSION
irb => "2.3.0"
irb > nil[1] = 1
irb => nil
irb > nil['foo'] = 'bar'
NoMethodError: undefined method `[]=' for nil:NilClass
from (irb):3
from /Users/spickermann/.rbenv/versions/2.3.0/bin/irb:11:in `<main>'
It seems like NilClass#[]= doesn't work properly in Ruby 2.3.0. Since it was completely removed in 2.3.1, I guess that this method or this behavior was added by accident.
Update: Cary Swoveland pointed out in a comment on another question that this behavior was a bug and was fixed in later versions (see: https://bugs.ruby-lang.org/issues/11976).

Ruby Gems "NameError: undefined local variable or method 'update' for main:Object" on every command

I'm attempting to set up a Watir environment. I had several issues actually installing the gems necessary, so I uninstalled and reinstalled Ruby 1.9.3 (I'm running Windows 7.) Now, I can't do any installs, updates, etc. from the ruby command line. Here is an example of some simple commands that should work but are not:
C:\Users\Matt Adams>irb
irb(main):001:0> gem -v
NameError: undefined local variable or method `v' for main:Object
from (irb):1
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):002:0> gem update
NameError: undefined local variable or method `update' for main:Object
from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
I can start ruby irb, but that's it. Its almost as if none of the ruby commands were installed. Anyone have any suggestions? Note that I've already done a re-install.
IRB is there for you to try out Ruby commands or snippets and see immediate responses. If you want to install or update gems, I suggest you get off IRB first by running "quit" and follow whatever instructions you have on your hand.

NoMethodError: undefined method 'perform' for nil:nilClass

NoMethodError: undefined method 'perform' for nil:nilClass
The message above is what I'm getting from my command line. This is after a type in my path and then the sass --watch input.scss:output.css then it returns the above error message.
SASS version: 3.2.14
Ruby version: 2.0.0 (The most recent version)
Windows 8
Using a localhost and a testing server.
I need help! :)
Hi this is "usually" a problem with your sass code and not with the compiler itself. Track back a few steps and see if you added any unusual code like IE filters etc.
You can refer here for more
Sass error on compilation NoMethodError: undefined method `count' for nil:NilClass Use --trace for backtrace

Time methods in Ruby

I'm trying to understand how method invocation works in Ruby objects. Ruby docs list a bunch of methods to execute on ruby objects. When I try one of them
puts RUBY_VERSION
puts Time.new(2008,6,21, 13,30,0, "+09:00").utc.seconds_since_midnight
I get the following output
1.9.3
bin/musor.rb:14:in `<main>': undefined method `seconds_since_midnight' for 2008-06-21 13:30:00 +0900:Time (NoMethodError)
What's wrong with the call I make?
seconds_since_midnight is Time extension added by rails. If you want to use it, you will need to add require 'activesupport/core_ext' and install activesupport gem first.

Resources