简体   繁体   中英

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
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

>> 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.

This should work for you in Ruby from 1.8 on to 2.0 and beyond.

According to the docs , require will look for start.rb in the $LOAD_PATH since it does not resolve to an absolute path. In this case, the LoadError is telling you that start.rb is not in your $LOAD_PATH .

I am guessing that start.rb is in your current directory. You can use

>> require '/path/to/start.rb'

or

>> require './start.rb'

or

>> require './start'

这可以节省您一些输入:

$ irb -r ./start.rb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM