After I installed rubygem on Linux, I found a problem. The ruby file written separately cannot use the package on the relative path require gem.
For example
Require 'something 'doesn't work. You must use the absolute path require'/usr/local.../something '.
I checked the loadpath ("$:") of Ruby and found that this path is not in the loadpath of Ruby. It is reasonable to say that require is not correct.
But why is it on my windows or rubyonrails?ProgramSo you can use require. After some Google tests, I finally found out why
It turns out that there is an environment variable rubyopt = 'rubygems 'in Windows XP but not in Linux. After rubyopt is added to Linux, it can work, it is further found that the require 'rubygems 'program can automatically identify the libraries in the GEM package as long as other require classes are involved. It seems that rubygems has made some effort on the require function, which is summarized as follows:
In rubyopt, 'rubygems 'actually references the file ubygems. RB, and then require 'rubygems' in ubygems. Rb is equivalent to making an alias
In rubygems. RB, the kernel module is adjusted as follows:
1. added the require_gem command, which supports require according to the specified version.
2. At the end of the file, require 'custom_require'
In custom_require.rb, The require function is modified,CodeAs follows:
Module Kernel
Alias require _ require
Def Require (PATH)
Require _ path
Rescue loaderror => Load_error
Begin
@ Gempath_searcher | = GEM: gempathsearcher. New
If Spec = @ Gempath_searcher.find (PATH)
Gem. Activate (spec. Name, true, " ##{ Spec. Version} " )
Require _ path
Else
Raise load_error
end
above code, the require function is updated. When the file cannot be read from the loadpath in ruby, require searches gempath, in this way, the require function can support the gem.
conclusion: to use the package in the gem, three methods
1 use rubyopt = 'rubygems '
2 first require 'rubygems' or 'ubygems '
3 Use require_gem