Ruby 'require' error: cannot load such file

后端 未结 13 1475
抹茶落季
抹茶落季 2020-12-02 05:07

I\'ve one file, main.rb with the following content:

require \"tokenizer.rb\"

The tokenizer.rb file is in the same directory and it

13条回答
  •  眼角桃花
    2020-12-02 05:41

    The problem is that require does not load from the current directory. This is what I thought, too but then I found this thread. For example I tried the following code:

    irb> f = File.new('blabla.rb')
    => #
    irb> f.read
    => "class Tokenizer\n    def self.tokenize(string)\n        return string.split(
    \" \")\n    end\nend\n"
    irb> require f
    LoadError: cannot load such file -- blabla.rb
            from D:/dev/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `req
    uire'
            from D:/dev/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `req
    uire'
            from (irb):24
            from D:/dev/Ruby193/bin/irb:12:in `
    '

    As it can be seen it read the file ok, but I could not require it (the path was not recognized). and here goes code that works:

    irb f = File.new('D://blabla.rb')
    => #
    irb f.read
    => "class Tokenizer\n    def self.tokenize(string)\n        return string.split(
    \" \")\n    end\nend\n"
    irb> require f
    => true
    

    As you can see if you specify the full path the file loads correctly.

提交回复
热议问题