Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if a required module came from a gem, or was a core module?

Tags:

ruby

If I see a require in source code.

require "blah"

How do I determine if that library ("blah") was provided by the core ruby installation or if it came from the installation of a gem?

like image 302
DragonFax Avatar asked Nov 03 '22 20:11

DragonFax


2 Answers

You can read the $LOADED_FEATURES and check if path came from gem or not, which means that you are testing if the feature belongs to the core installation or not.

# return true if library is an external gem
$LOADED_FEATURES.grep(/library/).grep(/gems/).size > 0
like image 106
Thiago Lewin Avatar answered Nov 08 '22 04:11

Thiago Lewin


Once require has found an loaded a library file it adds the full path to that file to the $LOADED_FEATURES array. So you could look in that array to see where it found blah

$LOADED_FEATURES.find_all { |path| puts path if /blah/ =~ path }
like image 28
Borodin Avatar answered Nov 08 '22 05:11

Borodin