irb returning NameError: uninitialized constant Date - ruby

ruby-v2.2.3 is supposed to have Date class preloaded into irb, however when I enter...
Date
NameError: uninitialized constant Date
from (irb):1
from /Users/noah/.rubies/ruby-2.2.3/bin/irb:11:in `'
Why should I have to require Date every single time if it's supposed to be preloaded into 2.2.3?

Date isn't listed as a core class in v2.2.3 or the current Ruby v2.3.1 core-classes, but Time is. Here's some IRb output:
$ irb -f
irb(main):001:0> Date.class
NameError: uninitialized constant Date
Did you mean? Data
from (irb):1
from /Users/ttm/.rbenv/versions/2.3.1/bin/irb:11:in `<main>'
irb(main):002:0> Time.class
=> Class
irb(main):003:0> Time.methods(false)
=> [:at, :now, :utc, :gm, :local, :mktime]
That is a limited subset of Time's methods though:
irb(main):002:0> require 'time'
=> true
irb(main):003:0> Time.methods(false)
=> [:at, :now, :utc, :gm, :local, :mktime, :parse, :zone_offset, :strptime, :rfc2822, :rfc822, :httpdate, :xmlschema, :iso8601]

Why do you say Date is preloaded? It's not a core class, it's part of the stdlib so it needs to be required. Time is a core class instead.

You can try to do the following in the beginning of your file:
require 'Date'

As far as I know, (and as Ursus said), Date is not preloaded.
Attention: So Tilec suggested to just load the library at the beginning of the file, which gave me (Not sure if that is true in general): this gave me LoadError: cannot load such file -- Date. Trying gem install Date gives me
ERROR: Could not find a valid gem 'Date' (>= 0) in any repository
ERROR: Possible alternatives: date
Solution: So I'm proposing to correct to lower case:
require 'date'

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)

undefined method `zone` for Time:Class after requiring active_support/time_with_zone

I'm on Ruby 2.2.1 and have active_support 4.2 installed so I want to use
Time.zone.parse('2007-02-10 15:30:45')
as described here: http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html
But even after requiring active_support and active_support/time_with_zone, I doesn't seem like Time.zone still doesn't work. Observe:
2.2.1 :002 > require "active_support"
=> true
2.2.1 :003 > Time.zone
NoMethodError: undefined method `zone' for Time:Class
2.2.1 :004 > require "active_support/time_with_zone"
=> true
2.2.1 :005 > Time.zone
NoMethodError: undefined method `zone' for Time:Class
What's going on?
The zone class method for the Time class is actually in active_support/core_ext/time/zones. You can require that class if you really want to use Time.zone but a better approach might to require active_support/all
Suggested Solution
require "active_support/all"
If you want to check out the source code fort for active_support look at the github repo
active_support/time_with_zone provides the ActiveSupport::TimeWithZone class, but it doesn't extend the core Time class.
You can require active_support/time instead:
require 'active_support'
require 'active_support/time'
Time.zone = 'Eastern Time (US & Canada)' #=> "Eastern Time (US & Canada)"
Time.zone.parse('2007-02-10 15:30:45') #=> Sat, 10 Feb 2007 15:30:45 EST -05:00
Try
require 'active_support/all'
instead of
require "active_support"

Uninitialized constant Builder (NameError) - Ruby

I need to use XmlMarkup.
In my script, I import "builder" but when I create an element I receive the error "uninitialized constant Builder (NameError)". Here the step that fails:
require 'rubygems/builder'
...
xml = Builder::XmlMarkup.new( :indent => 2) ===> uninitialized constant Builder (NameError)
I tried also using other syntax, like:
::Builder::XmlMarkup.new( :indent => 4 )
but I received the same error
Write as below, as Usage is telling :
require "builder" # when your ruby version is 1.9 or greater.
Uninitialized name error means you spelled the class wrong. XmlMarkup is case sensitive
In my case i had to do:
require "builder/xmlmarkup"

Ruby. Crack gem. -- in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) --

Dear stackoverflow community,
Beginner's question:
Why do I get the following error?
scraper_sample_2.rb:7:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
>Exit code: 1
Here's my code (copied from a ruby's intro guide):
require "rubygems"
require "crack"
require "open-uri"
URL = "http://www.recovery.gov/pages/GetXmlData.aspx?data=recipientHomeMap"
Crack::XML.parse(open(URL).read)["totals"]["state"].each do |state|
puts ["id", "awarded", "received", "jobs"].map{|f| state[f]}.join(",")
end
Because Crack::XML.parse(open(URL).read)["totals"] is nil. Try to split the call you do on line 7 on several lines and debug each call separately. Maybe the answer you get is not what you expect.
Given the format of the xml returned from your source, Crack::XML.parse(open(URL).read)["totals"] will, as Ivaylo said, return nil. The format of the xml must have changed, as totals are now within /map/view.
To get the expected output, change your code to:
Crack::XML.parse(open(URL).read)["map"]["view"]["totals"]["state"].each do |state|
puts ["id", "awarded", "received", "jobs"].map{|f| state[f]}.join(",")
end

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