Small details of rubygem

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.