Ruby require local file problem

Discussion in 'Ruby' started by Svin Svin, Apr 29, 2010.

  1. Svin Svin

    Svin Svin Guest

    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>'
    --
    Posted via http://www.ruby-forum.com/.
    Svin Svin, Apr 29, 2010
    #1
    1. Advertising

  2. Svin Svin

    Luis Lavena Guest

    On Apr 29, 2:34 pm, Svin Svin <> wrote:
    > 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?
    --
    Luis Lavena
    Luis Lavena, Apr 29, 2010
    #2
    1. Advertising

  3. Svin Svin

    Svin Svin Guest


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

    --
    Posted via http://www.ruby-forum.com/.
    Svin Svin, Apr 29, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lyndon
    Replies:
    1
    Views:
    509
    Leonard Blaisdell
    Jul 25, 2005
  2. cungar
    Replies:
    0
    Views:
    367
    cungar
    Nov 8, 2007
  3. Grehom
    Replies:
    10
    Views:
    357
    Grehom
    Dec 19, 2005
  4. Jean-denis Vauguet
    Replies:
    3
    Views:
    92
    Jean-denis Vauguet
    Mar 24, 2010
  5. Marc Girod

    get local packages symbols with require

    Marc Girod, May 22, 2012, in forum: Perl Misc
    Replies:
    9
    Views:
    461
    Rainer Weikusat
    May 25, 2012
Loading...

Share This Page