Ruby does not have a strict package management mechanism like Java, so it is confusing to reference it. Some people say that Ruby is not suitable for large projects with multi-person collaboration, which is reasonable.
Ruby loads other classes by using require, for example:
Ruby Code
- Require'Dbi'
- Require"Rexml/document"
However, the above files are loaded in the standard class library. Of course, they can also be installed gems files. However, if they are locally written files, you cannot directly use require, if there is a hello. the RB file must reference the print of the current directory. the class or method in the Rb file cannot use require, but should be like this:
Ruby code
- Require_relative"Print"
However, if you want to directly reference the file by using require, you can use the current directory as the path for Ruby loading in the file header:
Ruby code
- $ Load_path. Unshift (File. Dirname (_ file __))Unless $ Load_path. Include? (File. Dirname (_ file __))
- Require"Print"
The file. dirname (_ file _) indicates the current path, and the $ load_path.unshift method aims to apply the current directory to the ruby standard loading path.
In general, the loading method of Ruby is not a big problem. Although it does not seem as concise as the Java package management mechanism, the package management mechanism of Java is indeed not suitable for the Dynamic Language of Ruby, it is too long, but Java can use the class of the current directory by default without the need for import. In this case, it is still a good feature.
**************************************** **************************************** **************************************** **************************************** *********************************
Require is generally used to load other classes, such:
# Ruby code:
Require 'dbi' require "rexml/document"
However, the above files are loaded in the standard class library. Of course, they can also be installed gems files,
However, if you are writing a file locally, you cannot directly use require,
This should be the case:
# E7.4-1. RB module (module) module module1 def SQRT (Num, RX = 1, E = 1e-10) num * = 1.0 (Num-RX * RX). ABS <e? RX: SQRT (Num, (Num/RX + RX)/2, e) endend
# E7.4-2. RB person class person def talk puts "I'm talking." endend
# Use require_relative to load the local Ruby file require_relative "E7.4-1" require_relative "E7.4-2" class student <person include module1endastudent = student. newastudent. talk # I'm talking. puts astudent. SQRT (8.77496438739435) #
# However, If You Want To directly reference the file by using require, you can use the current directory as the path for Ruby loading in the file header:
# File. dirname (_ file _) indicates the current path, and the $ load_path.unshift method aims to apply the current directory to the ruby standard loading path.
$ Load_path.unshift (file. dirname (_ file _) Unless $ load_path.include? (File. dirname (_ file _) require "E7.4-1" require "E7.4-2" class student <person include module1endastudent = student. newastudent. talk # I'm talking. puts astudent. SQRT (8.77496438739435) #
$ Load_path.unshift (file. dirname (_ file _) Unless $ load_path.include? (File. dirname (_ file _) # change this value to $: <. $: <'.'
File. expand_path (file. Join (file. dirname (_ file _), '...', '...', 'page', 'client _ arithmetic '))
1. $ load_path refers to an environment variable for Ruby to read external files. In fact, it is a concept with Windows environment variables. Ruby will read the require file in the path of the environment variable. If the file cannot be found in the environment variable, A loaderror error will be reported. $ Load_path and $: Indicate the same environment variable, which is the same.
2. _ file _ refers to the relative location of the directory where the current RB file is located.
3. file. Join is a method that combines its own parameters into a directory. For example, the preceding method is "xxx/http://www.cnblogs.com/page/client_arithmetic" (relative location of the XXX code RB file)
4. file. expand_path is the method for converting its parameters from the relative path to the absolute path.
5. $ load_path.unshift adds the absolute path obtained above to the environment variables that already exist.