Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know what Ruby standard libraries I need to explicitly require?

Tags:

ruby

I've been looking at the Ruby Standard Library Documentation for 1.9.3. I'm wondering how I can find out which libraries I need to explicitly require.

For example, I have been told I need to explicitly require the json library, but not rubygems, although they both appear in the standard library list.

Is there another place I can look for information about the requirements, or even a pry/irb command I can run to see what is already 'required'?

like image 662
MothOnMars Avatar asked Nov 01 '25 08:11

MothOnMars


1 Answers

Every library has to be required/loaded, if you want to use it.

Performance-wise, require is smart and will not load libraries if they're already loaded.

However, if you want to check if a library is required you should see the top-level namespace it defines and check if this is defined in your current scope. For example open an IRB session and do:

defined? JSON  # => nil (it's not loaded)
require 'json' # => true (load it)
defined? JSON  # => "constant" (it's loaded)
require 'json' # => false (it's already loaded, so it's not loaded it again)
like image 65
Agis Avatar answered Nov 04 '25 04:11

Agis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!