19

Ruby Doc has two sections: Core and Standard. Core comes by default and standard has additional libraries/methods etc. Does it mean I have to require these standard libraries in order to use them? I thought so and picked DateTime.now from standard library without requiring anything, and it worked.

2
  • 1
    Your description is wrong. DateTime.now returns an error without require "date" or require "time".
    – sawa
    Commented Aug 29, 2013 at 8:35
  • @sawa: you are right, I didn't fireup a new irb and used the one which I was playing with. So I might have loaded some other library.
    – Bala
    Commented Aug 29, 2013 at 8:46

1 Answer 1

18

Yep, you got it right. Core functionality is everything you don't have to require to use.

DateTime seems to be not in the core (are you running your line inside of rails console, maybe?)

DateTime.now # => 
# ~> -:1:in `<main>': uninitialized constant DateTime (NameError)

But Time is

Time # => Time
Time.now # => 2013-08-29 12:32:54 +0400

Only a few methods of Time are in core, though. To get more functionality (like Time.parse) you have to

require 'time'
3
  • OP is asking something different - I thought so and picked DateTime.now from standard library without requiring anything, and it worked. But for me it is not the case... I don't know how OP did that. I am in Ruby 1.9.3... Commented Aug 29, 2013 at 8:36
  • My guess is that he tried it in rails console (or some other "loaded" IRB session). DateTime is not in the core. Commented Aug 29, 2013 at 8:38
  • @SergioTulentsev and @ Babai : I have wasted your time and my time, trying the same in new irb threw an error.
    – Bala
    Commented Aug 29, 2013 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.