[英]RUBY 2.0.0 How to run a file in irb, so that all its methods are parts of irb environment?
I know the command for this is: require file name 我知道此命令是:要求文件名
but I guess this command is for ruby 1.9.4 and I am using ruby 2.0.0 但是我猜这个命令是针对Ruby 1.9.4的,我正在使用Ruby 2.0.0
the exact message is. 确切的消息是。
$require start.rb
LoadError: cannot load such file -- start.rb
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from (irb):2
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'
Thanks in advance 提前致谢
Inside irb 内部irb
>> require './start'
=> true
The file start.rb being in the same folder as you are, and the required file being in quotes, prefaced with the relative location ./
as shown. 文件start.rb与您所在的目录位于同一文件夹中,并且所需文件用引号引起来,并带有相对位置
./
开头,如图所示。
This should work for you in Ruby from 1.8 on to 2.0 and beyond. 在Ruby从1.8到2.0及更高版本中,这应该对您有用。
According to the docs , require
will look for start.rb
in the $LOAD_PATH
since it does not resolve to an absolute path. 根据文档 ,
require
会在$LOAD_PATH
查找start.rb
,因为它不能解析为绝对路径。 In this case, the LoadError
is telling you that start.rb
is not in your $LOAD_PATH
. 在这种情况下,
LoadError
告诉您start.rb
不在$LOAD_PATH
。
I am guessing that start.rb
is in your current directory. 我猜
start.rb
在您的当前目录中。 You can use 您可以使用
>> require '/path/to/start.rb'
or 要么
>> require './start.rb'
or 要么
>> require './start'
这可以节省您一些输入:
$ irb -r ./start.rb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.