$LOAD_PATH in Ruby
这篇文章搞定了Ubuntu上的RMagick
$LOAD_PATH /Library/Ruby/Site/1.8
# => /Library/Ruby/Site/1.8/powerpc-darwin9.0
# => /Library/Ruby/Site/1.8/universal-darwin9.0
# => /Library/Ruby/Site
# => /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8
# => /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/powerpc-darwin9.0
# => /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin9.0
# => .
That’s the load path on my OS X system. A bunch of directories, and . (dot). The dot represents the current working directory.
Let’s investigate
# ~/foo/top_level.rb
puts "I rule the world"
require "underlings/slave"
# ~/foo/underlings/slave.rb
puts "I am a slave."
require "equal"
# ~/foo/underlings/equal.rb
puts "I am equal."
Close your eyes. You open a terminal. The current working directory is ~/foo.
$ ~/foo: ruby top_level.rb
I rule the world!
I am a slave.
./underlings/slave.rb:3:in `require': no such file to load -- equal (LoadError)from ./underlings/slave.rb:3
from top_level.rb:3:in `require'
from top_level.rb:3
Ruby looks for equal in the directories in the load path.
~/foo/underlings isn’t in the load path, which means Ruby can’t find equal.rb.
It raises a LoadError.
$ ~/foo: cd underlings
$ ~/foo/underlings: ruby slave.rb
I am a slave.
I am equal.
This time, Ruby finds equal. That’s because the curent working directory is the directory that contains equal.rb, and Ruby adds the current working directory to the load path.
Modifying the load path
Let’s add ~/foo/underlings to the load path.
# ~/foo/top_level.rb
puts "I rule the world"
$LOAD_PATH << "~/foo/underlings"
require "underlings/slave"
Back to the imaginary terminal.
$ ~/foo/underlings: cd ..
$ ~/foo: ruby top_level.rb
I rule the world!
I am a slave.
I am equal.
Modifying the load path in a sensible way
# ~/foo/top_level.rb
puts "I rule the world"
$LOAD_PATH << File.join(File.dirname(__FILE__), "underlings")
require "slave"
- File.join is a cross platform file path generator. Platforms vary between using forward and backwards slashes to delimit directories, so you want to do
File.join('foo', 'bar')
, not "foo/bar"
, when referring to directories.
- Instead of adding the relative ~/foo/underlings,
File.dirname(__FILE__)
is used. It returns the absolute path to the current file.
- Because ~/foo/underlings is in the load path,
require "underlings/slave"
is pointless. require "slave"
instead.
That $: thing
$:
is a shorthand for $LOAD_PATH
. So now you know.
(In fact, $LOAD_PATH
is longhand for $:
. Yeah! Thanks, bryanl.)
Add directories to the beginning of your load path
Speed is some times convenient. If you have a lot of requires to your own load-pathed directories, you might want to add your directory to the beginning of the load path, so that Ruby will look in your own directories before it starts traversing through the system directories.
The load path is an array, Array#unshift to the rescue.
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'foo'))
And there you go.
分享到:
相关推荐
zozo 是一个工具,通过让它们在运行时不加载 rubygems/bundler,可以轻松减少应用程序的内存占用: $ unicorn -c unicorn.conf -D $ ps ux USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND jeremy ...
`require`背后的机制涉及到了Ruby的加载路径(`$LOAD_PATH`),这是一个Ruby用来搜索文件或库的目录列表。 #### `require`与`require_relative` 在Ruby中,除了`require`之外,还有一个`require_relative`关键字。这...
- 当使用`require`时,Ruby解释器会根据`$LOAD_PATH`环境变量中的路径来查找指定的文件。 - `$LOAD_PATH`是一个数组,包含了Ruby解释器搜索库文件的所有路径。 - 如果文件不在`$LOAD_PATH`中,则Ruby会抛出一个...
入门与教程 示例扩展 我们经常发现,我们需要的培训水平不仅要超出基础知识,而且还比仅仅阅读API文档提供更多信息。 别再看了! 请查看我们的“示例扩展”部分,以获取功能齐全的SketchUp扩展的示例,并附带注释和...
此命令从$LOAD_PATH或rubygems检测并打开一个库。 reditor需要$EDITOR变量。 安装 $ gem install reditor 用法 打开标准库(仅纯Ruby) $ reditor set 打开宝石 $ reditor railties 通过$ SHELL打开图书馆 $ ...
为$LOAD_PATH正确性,每次调用Bootscale.regenerate修改$LOAD_PATH都应更新缓存。 对于Rails应用程序,这意味着在config/application.rb添加一个初始化config/application.rb 。 module MyApp class Application ...
CloudformationMapper Cloudformation 的.../usr/local/env ruby $LOAD_PATH << File . join ( File . dirname ( __FILE__ ) , 'lib' ) Dir [ File . join ( File . dirname ( __FILE__ ) , "lib" , "cloudforma
使用以下内容创建配方: include_recipe "chef_handler"# Install `chef-handler-profiler` gem during the compile phasechef_gem "chef-handler-profiler"# load the gem here so it gets added to the $LOAD_PATH,...
Zeitwerk 介绍 Zeitwerk是用于Ruby的高效且线程安全的代码加载器。 给定,Zeitwerk能够按需(自动加载)或... 在内部,Zeitwerk问题require仅使用绝对文件名require调用,因此$LOAD_PATH中没有昂贵的文件系统查找。
$LOAD_PATH require 'trig' y = Trig.sin(Trig::PI / 4) ``` #### 七、include 语句 通过`include`关键字,可以将一个模块的内容混入到类中。这意味着类可以访问模块中定义的所有公共方法。 **语法**: ```ruby ...
graph = YAML.load_file('test_data/test1.yml') colony = AntColony::Colony.new(graph, beta: 0.8, alpha: 0.7, pop: 200, ph: 0.3, q: 5) colony.solve colony.find_path 1 # path from point 1 更多详情、使用...
MiniI18n Ruby的简约I18n库 MiniI18n是一个简单,灵活和快速的Ruby国际化库。 它支持本地化,内插,复数,后备,嵌套键等。 翻译应存储在YAML或JSON文件中... # Path to your translation files. config . load_tra
5. **模块和类的加载路径**:引入了`$LOAD_PATH`(后来更改为`$:`)的概念,用于控制类和模块的加载顺序和位置。 RubyInstaller-1.9.2-p0.exe 是这个安装程序的可执行文件,它包含了安装过程中所需的所有组件,包括...
该演示API实现了领域级的,基于隔离的,基于模块的类(带有Application[...]访问器),这些类仅在非常基本的require和$LOAD_PATH逻辑下才在运行时加载。 系统依赖项或庞大的第三方库在“需要时”加载,以确保应用...
Ruby(不带Rails)如果要在不带Rails的情况下使用此库,则只需将i18n添加到Gemfile中:gem'i18n',然后使用一些翻译和默认语言环境配置I18n:I18n.load_path << Dir [File.expand_path (“配置/语言环境”)+...
load_path << Dir [ File . expand_path ( "config/locales" ) + "/*.yml" ] I18n . default_locale = :en # (note that `en` is already the default!) 您项目中的一个简单翻译文件可能位于config/locales/...
Ruby猛男 一个用于检查和... filetype # => :execute# get all load commands in the file and print their offsets:file . load_commands . each do | lc | puts " #{ lc . type } : offset #{ lc . offset } , size
Ruby on Rails 是一个广受欢迎的Web开发框架,它简化了构建功能丰富的Web应用程序的过程。在开发过程中,确保用户只能访问他们被授权查看或操作的数据是非常关键的,这就是授权(Authorization)的角色所在。...
Rjb::load('path/to/your/library.jar') # 创建Java对象 tar_utils = Rjb::import('com.example.util.TarUtils').new # 调用Java方法 archive_path = 'path/to/archive.tar' extract_path = 'path/to/extract' tar...