Ruby 中的“$”字符是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1896876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the "$" character mean in Ruby?
提问by Scott Radcliff
Been playing with Ruby on Rails for awhile and decided to take a look through the actual source. Grabbed the repo from GitHub and started looking around. Came across some code that I am not sure what it does or what it references.
使用 Ruby on Rails 一段时间后决定查看实际源代码。从 GitHub 上获取 repo 并开始环顾四周。遇到一些我不确定它做什么或引用什么的代码。
I saw this code in actionmailer/test/abstract_unit.rb
我在 actionmailer/test/abstract_unit.rb 中看到了这段代码
root = File.expand_path('../../..', __FILE__)
begin
require "#{root}/vendor/gems/environment"
rescue LoadError
$:.unshift("#{root}/activesupport/lib")
$:.unshift("#{root}/actionpack/lib")
end
lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib)
require 'rubygems'
require 'test/unit'
require 'action_mailer'
require 'action_mailer/test_case'
Can someone tell me what the $: (a.k.a. "the bling") is referencing?
有人能告诉我 $:(又名“金光闪闪”)指的是什么吗?
采纳答案by Mark Byers
$: is the global variable used for looking up external files.
$: 是用于查找外部文件的全局变量。
From http://www.zenspider.com/Languages/Ruby/QuickRef.html#18
来自http://www.zenspider.com/Languages/Ruby/QuickRef.html#18
$: Load path for scripts and binary modules by load or require.
$:通过 load 或 require 加载脚本和二进制模块的路径。
回答by Justin Love
$identifies a global variable, as opposed to a local variable, @instance variable, or @@class variable.
$标识全局变量,而不是局部变量、@instance 变量或@@class 变量。
Among the language-supplied global variables are $:, which is also identified by $LOAD_PATH
在语言提供的全局变量中有$:,它也由$LOAD_PATH
回答by Alex Moore-Niemi
I wanna note something weird about Ruby!
我想注意Ruby的一些奇怪之处!
$does indeed mean load path. And ;means "end line". But!
$确实意味着加载路径。并;意味着“结束线”。但!
$;means field separator. Try running $;.to_sin your REPL and you'll see it return ",". That's not all! $with other suffixes can mean many other things.
$;表示字段分隔符。尝试$;.to_s在您的 REPL 中运行,您会看到它返回","。那不是全部!$带有其他后缀可以表示许多其他的东西。
为什么?好吧,当然是 Perl!
回答by Justin Ethier
To quote the Ruby Forum:
引用Ruby 论坛:
ruby comes with a set of predefined variables
ruby 带有一组预定义的变量
$: = default search path (array of paths)
__FILE__ = current sourcefile
if i get it right (not 100% sure) this adds the lib path to this array of search paths by going over the current file. which is not exactly the best way, i would simply start with RAILS_ROOT (at least for a rails project)
如果我做对了(不是 100% 确定),这会通过遍历当前文件将 lib 路径添加到此搜索路径数组。这不是最好的方法,我只是从 RAILS_ROOT 开始(至少对于 rails 项目而言)
回答by TK.
$:.unshift
is the same as
是相同的
$LOAD_PATH.unshift
. You can also say:
. 你也可以说:
$: <<
$LOAD_PATH <<
They are pretty common Ruby idioms to set a load path.
它们是用于设置加载路径的非常常见的 Ruby 习惯用法。

