Where is Float#to_d? - ruby

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'

Related

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

Unable to open excel with Roo or Spreadsheet Gems

When I try this with roo gem:
irb(main):001:0> require 'roo'
=> true
irb(main):002:0> oo = Excel.new("C:/Users/Abash/Desktop/test1.xls")
**NameError: uninitialized constant Excel** from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
When I try this with spreadsheet gem:
irb(main):001:0> require 'spreadsheet'
=> true
irb(main):002:0>Spreadsheet.client_encoding = 'UTF-8'
=> "UTF-8"
irb(main):003:0> book = Spreadsheet.open 'C:/Users/Abash/Desktop/test1.xls'
**Errno::EACCES: Permission denied** - C:/Users/Abash/Desktop/test1.xls
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `initialize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `open'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `open'
from (irb):3
from C:/Ruby193/bin/irb:12:in `<main>'
Can somebody show me a work around for these errors?
I believe you are using ruby 1.9 or above version. In such a case,you need to specify the gem while creating a new instance.
require 'roo'
s =Roo::Excel.new("myspreadsheet.xls")
s =Roo::Excelx.new("myspreadsheet.xlsx")

How to reproduce undefined method error for NilClass

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>'

Why is Ruby's Date class automatically loaded but DateTime is not?

Using IRB, why are the Date & Time classes automatically loaded, but DateTime is not? I have to require 'date', this does not make sense to me because I thought that both Date and DateTime were using the standard library 'date'?
ruby-1.9.2-p290 :001 > Date
=> Date
ruby-1.9.2-p290 :002 > Time
=> Time
ruby-1.9.2-p290 :003 > DateTime
NameError: uninitialized constant Object::DateTime
from (irb):3
from /Users/kamilski81/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby-1.9.2-p290 :004 > require 'date'
=> true
ruby-1.9.2-p290 :005 > require 'date'
=> false
ruby-1.9.2-p290 :006 > DateTime
=> DateTime
In IRB, include this line: require 'date' then you will be able to use DateTime.
irb(main):000:0> DateTime.class
NameError: uninitialized constant DateTime
from (irb):0
from /path/to/ruby/irb:12:in '(main)'
irb(main):001:0> require 'date'
=> true
irb(main):002:0> DateTime.class
=> Class
Worked for me when first initializing with require 'date'.
Being a little more curious, I tried:
$ ruby -e 'puts DateTime.class'
-e:1:in `<main>': uninitialized constant Object::DateTime (NameError)
[~, kamilski81#mac]
$ ruby -e 'puts Date.class'
-e:1:in `<main>': uninitialized constant Object::Date (NameError)
$ ruby -e 'puts Time.class'
Class
So it makes me think that it's an irb issue that automatically loads 'date'.

Resources