Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Core and Standard Library Ruby Standard Library: Part 1 The Ruby Time Class

Oğulcan Girginc
Oğulcan Girginc
24,848 Points

Why do we need require "time"?

I thought, we don't need to use require, when the class is in the core library. Am I wrong?

When you get code from other files, you have to require the file to tell Ruby that you are going to use that file. Without that, each time you run the program, it takes FOREVER for Ruby to find that one file.

The Time gem is a separate file, because there is a website (I forgot the name...) that has LOADS of gems, created by regular people like you and me. Ruby can't handle SO many gems so quickly, so they made it so you have to require it.

Hope you get it! ~xela888

Oğulcan Girginc
Oğulcan Girginc
24,848 Points

Hi xela888

Hmm... but isn't Time a core class of Ruby? I mean, it's literally part of the Ruby-Doc.org/Core.

Oooh, now I remember Jason Seifer used the Time gem without requiring it... Maybe the time gem is a built-in class and not a gem...

Then maybe gems are in the Core Library. ¯\ (._.) /¯ (Don't know)

There is a Time class in the standard library, but there is only a few methods you can use with it. Then there is an additional class in the core library which needs to be required. Jason mentions it right at the start of the video around the 1 minute mark

1 Answer

Time is part of the ruby core so you don't need to do require 'time'. It works just fine. This article explains it in greater detail.

For better understanding I suggest you run

puts Time.new(2002) 
puts $LOAD_PATH
puts "*" * 10
puts $LOADED_FEATURES

and

require 'time'
puts Time.new(2002) 
puts $LOAD_PATH
puts "*" * 10
puts $LOADED_FEATURES

You will notice the difference.