Ruby require local file problem

S

Svin Svin

I have two files foo.rb and test.rb in the same folder.

test.rb
------------------------------------
puts RUBY_VERSION
require 'foo'
------------------------------------

foo.rb
------------------------------------
module Foo
PATTT = "2323"
class Foo2
def f
puts 'hello world'
end
end
end

puts 'eee'
------------------------------------



Running under NetBeans it's OK. But running test.rb through concole OR
RubyMine fails with:


C:\USERS\ADMIN>ruby
C:\Users\Admin\Documents\NetBeansProjects\RubyApplication1\l
ib\test.rb
1.9.1
C:/Users/Admin/Documents/NetBeansProjects/RubyApplication1/lib/test.rb:2:in
`req
uire': no such file to load -- foo (LoadError)
from
C:/Users/Admin/Documents/NetBeansProjects/RubyApplication1/lib/test
rb:2:in `<main>'
 
L

Luis Lavena

I have two files foo.rb and test.rb in the same folder.

test.rb
------------------------------------
puts RUBY_VERSION
require 'foo'
------------------------------------

foo.rb
------------------------------------
module Foo
  PATTT = "2323"
  class Foo2
    def f
      puts 'hello world'
    end
  end
end

puts 'eee'
------------------------------------

Running under NetBeans it's OK. But running test.rb through concole OR
RubyMine fails with:

C:\USERS\ADMIN>ruby
C:\Users\Admin\Documents\NetBeansProjects\RubyApplication1\l
ib\test.rb
1.9.1
C:/Users/Admin/Documents/NetBeansProjects/RubyApplication1/lib/test.rb:2:in
`req
uire': no such file to load -- foo (LoadError)
        from
C:/Users/Admin/Documents/NetBeansProjects/RubyApplication1/lib/test
rb:2:in `<main>'

Simple: you're trying to require 'foo' from a directory that is not
the current one.

If both test.rb and foo.rb are located in the same directory, doing
"ruby path\to\test.rb" will not be able to find foo in the same path.

Now, if you CD into the directory test.rb and foo.rb are located,
require 'foo' will succeed, since the current directory is in the
$LOAD_PATH.

That helps?
 
S

Svin Svin

Simple: you're trying to require 'foo' from a directory that is not
the current one.

If both test.rb and foo.rb are located in the same directory, doing
"ruby path\to\test.rb" will not be able to find foo in the same path.

Now, if you CD into the directory test.rb and foo.rb are located,
require 'foo' will succeed, since the current directory is in the
$LOAD_PATH.

That helps?
Thanks, it helps!

Why it's so different from python/java :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top