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

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

Related

irb returning NameError: uninitialized constant Date

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'

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"

NoMethodError: undefined method `zone' for Time:Class

How do i use Time.zone in ruby if I am not using rails
I want to do Time.now but that's available in rails but not ruby
I thought that
require 'time'
would fix this and make it available in ruby but it didn't and I get
NoMethodError: undefined method `zone' for Time:Class
I don't know, what do you mean. But I think it should work as below :
(arup~>~)$ pry --simple-prompt
>> Time.now
=> 2014-04-09 23:19:04 +0530
>> Time.now.strftime('%Z')
=> "IST"
>> Time.now.strftime('%z')
=> "+0530"
>> Time.now.zone
=> "IST"
Documentation : #strftime and #zone .
You've tried to use zone as if it were a class method (Time.zone) [1]. If you want to use a class method:
1.9.3-p448 :007 > Time.now.zone
=> "EDT"
But Time.now is just a nice way of instantiating your own instance of Time[2]. So you're really just doing this (calling an instance method):
1.9.3-p448 :009 > time = Time.new
=> 2014-04-09 15:14:01 -0400
1.9.3-p448 :010 > time.zone
=> "EDT"
[1] http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/
[2] http://ruby-doc.org/core-1.9.3/Time.html#method-c-now

How do I test Digest::SHA2.hexdigest in online irb?

I want to see the hash created by the function Digest::SHA2.hexdigest. I do not have Ruby installed, so I went for the online irb. Typing
Digest::SH­A2.hexdige­st("hello"­)
gives
=> #<NameError: uninitialized constant Digest>
Is it possible to add the needed library in any online irb?
You need to do as below :
2.0.0-p0 :003 > require 'digest'
=> true
2.0.0-p0 :004 > Digest::SHA2.hexdigest("hello")
=> "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
2.0.0-p0 :005 >
So do first require 'digest'.
Try in tutorialspoint

Ruby Date Methods Issue

ruby-1.9.2-p290 :001 > require 'date' => true
ruby-1.9.2-p290 :002 > date = '01/23/2011'=> "01/23/2011"
ruby-1.9.2-p290 :003 > Date.strptime(date, "%m/%d/%Y") =>
#<Date: 2011-01-23 (4911169/2,0,2299161)>
ruby-1.9.2-p290 :004 > Date.mon
NoMethodError: undefined method `mon' for Date:Class
from (irb):4
from /Users/noahclark/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby-1.9.2-p290 :005 >
Why does it do this? I've looked in the documentation and .mon or .month is a valid method.
Thanks in advance.
mon and month are valid methods of Date instances. You're calling them on Date - that is, a class.
I suggest you take a look at the difference between class and instance methods.
strptime belongs to the former, mon to the latter.

Resources