ruby LoadError: cannot load such file

前端 未结 5 1887
抹茶落季
抹茶落季 2020-12-15 05:54

When I require a file, for example (called st.rb):

require \'rubygems\'
require \'mongrel\'

class TestHandler < Mongrel::HttpHandler

  def process(reque         


        
相关标签:
5条回答
  • I created my own Gem, but I did it in a directory that is not in my load path:

    $ pwd
    /Users/myuser/projects
    $ gem build my_gem/my_gem.gemspec
    

    Then I ran irb and tried to load the Gem:

    > require 'my_gem'
    LoadError: cannot load such file -- my_gem
    

    I used the global variable $: to inspect my load path and I realized I am using RVM. And rvm has specific directories in my load path $:. None of those directories included my ~/projects directory where I created the custom gem.

    So one solution is to modify the load path itself:

    $: << "/Users/myuser/projects/my_gem/lib"
    

    Note that the lib directory is in the path, which holds the my_gem.rb file which will be required in irb:

    > require 'my_gem'
     => true 
    

    Now if you want to install the gem in RVM path, then you would need to run:

    $ gem install my_gem
    

    But it will need to be in a repository like rubygems.org.

    $ gem push my_gem-0.0.0.gem
    Pushing gem to RubyGems.org...
    Successfully registered gem my_gem
    
    0 讨论(0)
  • 2020-12-15 05:57

    The problem shall have solved if you specify your path.

    e.g.

    "require 'st.rb'" --> "require './st.rb'"

    See if your problem get solved or not.

    0 讨论(0)
  • 2020-12-15 05:58

    The directory where st.rb lives is most likely not on your load path.

    Assuming that st.rb is located in a directory called lib relative to where you invoke irb, you can add that lib directory to the list of directories that ruby uses to load classes or modules with this:

    $: << 'lib'
    

    For example, in order to call the module called 'foobar' (foobar.rb) that lives in the lib directory, I would need to first add the lib directory to the list of load path. Here, I am just appending the lib directory to my load path:

    irb(main):001:0> require 'foobar'
    LoadError: no such file to load -- foobar
            from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
            from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
            from (irb):1
    irb(main):002:0> $:
    => ["/usr/lib/ruby/gems/1.8/gems/spoon-0.0.1/lib", "/usr/lib/ruby/gems/1.8/gems/interactive_editor-0.0.10/lib", "/usr/lib/ruby/site_ruby/1.8", "/usr/lib/ruby/site_ruby/1.8/i386-cygwin", "/usr/lib/ruby/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/i386-cygwin", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/i386-cygwin", "."]
    irb(main):004:0> $: << 'lib'
    => ["/usr/lib/ruby/gems/1.8/gems/spoon-0.0.1/lib", "/usr/lib/ruby/gems/1.8/gems/interactive_editor-0.0.10/lib", "/usr/lib/ruby/site_ruby/1.8", "/usr/lib/ruby/site_ruby/1.8/i386-cygwin", "/usr/lib/ruby/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/i386-cygwin", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/i386-cygwin", ".", "lib"]
    irb(main):005:0> require 'foobar'
    => true
    

    EDIT Sorry, I completely missed the fact that you are using ruby 1.9.x. All accounts report that your current working directory has been removed from LOAD_PATH for security reasons, so you will have to do something like in irb:

    $: << "."
    
    0 讨论(0)
  • 2020-12-15 06:00

    For security & other reasons, ruby does not by default include the current directory in the load_path. You may want to check this for more details - Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?

    0 讨论(0)
  • 2020-12-15 06:15

    I just came across a similar problem. Try

    require './st.rb'
    

    This should do the trick.

    0 讨论(0)
提交回复
热议问题