How to reproduce undefined method error for NilClass - ruby

I want to find out how to reproduce the following error in Ruby 1.9:
NoMethodError (undefined method `[]' for nil:NilClass):
It's my own interest. The following doesn't work for me:
a = nil
a[:key]
It produce the following error:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

Your code works on my machine (submitting an answer since I can't format the comments):
⇨ irb
1.9.3p194 :001 > a = nil
=> nil
1.9.3p194 :002 > a[:key]
NoMethodError: undefined method `[]' for nil:NilClass
from (irb):2
from /Users/bjc/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

Related

Why is RSpec giving a NoMethodError when method exists?

I'm getting the error below
$ bundle exec rspec calculatorcli_spec.rb
Failure/Error: c = CalculatorCLI.parse
NoMethodError:
undefined method `parse' for CalculatorCLI:Class
from the below simple calculator class
CalculatorCLI is a class, and you're missing its initialization
either change CalculatorCLI.parse to CalculatorCLI.new.parseor changedef parsetodef self.parseinCalculatorCLI` definition

Figure out where a method was defined

If I follow the following part of this article:
Figure out where a method was defined
object = Object.new
puts object.method(:blank?).source_location
=> ["/gems/activesupport-5.0.0.beta1/lib/active_support/core_ext/object/blank.rb", 14]
I should be able to find the definition of the blank? method, however when I try this code within irb with ruby 2.0.0 I get this error message:
➜ ~ irb
irb(main):001:0> object = Object.new
=> #<Object:0x007fc84882f088>
irb(main):002:0> puts object.method(:blank?).source_location
NameError: undefined method `blank?' for class `Object'
from (irb):2:in `method'
from (irb):2
from /usr/bin/irb:12:in `<main>'
Did I miss anything?
Thank you.
.blank? method does not exist for a Object type. I know for sure it exists for a String method if I include the active_support lib
irb(main):001:0> String.new.method(:blank?).source_location
=> ["/home/xeon/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object/blank.rb", 116]
If you include activesupport-5.0.0.beta1 then it will work for you. (Looking at the source path of the article you have posted)

Accessing httparty API results in ruby

I'm trying to access the API results from HTTParty, but can't figure out how...I've stored the results in a variable called zillow that looks like the following:
=> {"searchresults"=>
{"request"=>{"address"=>"49 Alpine Way", "citystatezip"=>"28805"},
"message"=>{"text"=>"Request successfully processed", "code"=>"0"},
"response"=>
{"results"=>
{"result"=>
{"zpid"=>"5628657",
"links"=>
{"homedetails"=>"http://www.zillow.com/homedetails/49-Alpine-Way-Asheville-NC-28805/5628657_zpid/",
"graphsanddata"=>"http://www.zillow.com/homedetails/49-Alpine-Way-Asheville-NC-28805/5628657_zpid/#charts-and-data",
"mapthishome"=>"http://www.zillow.com/homes/5628657_zpid/",
"comparables"=>"http://www.zillow.com/homes/comps/5628657_zpid/"},
"address"=>
{"street"=>"49 Alpine Way",
"zipcode"=>"28805",
"city"=>"Asheville",
I'm trying to access "zpid" but keep getting nil as a response. I've tried the following:
[107] pry(main)> zillow["searchresults"]["zpid"]
=> nil
[108] pry(main)> zillow["zpid"]
=> nil
[109] pry(main)> zillow["searchresults"]["results"]["zpid"]
NoMethodError: undefined method `[]' for nil:NilClass
from (pry):100:in `<main>'
[110] pry(main)> zillow.find["zpid"]
NoMethodError: undefined method `[]' for #<Enumerator:0x0000000323b3f8>
from (pry):101:in `<main>'
[111] pry(main)> zillow.get["zpid"]
NoMethodError: undefined method `get' for #<HTTParty::Response:0x00000003adb0f8>
from /home/pjw/.rvm/gems/ruby-2.3.0/gems/httparty-0.14.0/lib/httparty/response.rb:85:in `method_missing'
[112] pry(main)> zillow["searchresults"]
=> {"request"=>{"address"=>"49 Alpine Way", "citystatezip"=>"28805"},
"message"=>{"text"=>"Request successfully processed", "code"=>"0"},
What am I missing?
You missed a chain of keys to get zpid
zillow["searchresults"]["response"]["results"]["result"]["zpid"]
If you on ruby 2.3.0 then you can use Hash#dig method

Test returns ArgumentError

I meant to type test and receive nil. However I receive this instead:
ArgumentError: wrong number of arguments (0 for 2..3)
from (irb):15:in `test'
from (irb):15
from /usr/bin/irb:12:in `<main>'
Does anyone know if I need to fix something?
I think this may be what you're after:
2.3.0 :003 > puts 'test'
test
=> nil

Where is Float#to_d?

Sometimes I see code using to_d. The ruby documentation even states there is a Float#to_d method. However, it's not in my version of ruby (ruby 1.9.3p263 (2012-08-23 revision 36792).
1.9.3p263 :001 > "0.0".to_d
NoMethodError: undefined method `to_d' for "0.0":String
from (irb):1
from /home/iblue/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'
1.9.3p263 :002 > 0.0.to_d
NoMethodError: undefined method `to_d' for 0.0:Float
from (irb):2
from /home/iblue/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'
1.9.3p263 :003 > 0.to_d
NoMethodError: undefined method `to_d' for 0:Fixnum
from (irb):3
from /home/iblue/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'
No to_d in Float, String or Fixnum. What's going on?
As stated in the example of the documentation you need
require 'bigdecimal'
require 'bigdecimal/util'

Resources