rubyrequirerakefile

How to correctly include other project files from Ruby rakefile


I am having trouble including other files to use within a Rake file. For example, I have a version.rb file that contains the version number that I want to refer to inside the rake file, also, I have a gem spec, specified in a .gemspec file, which I also want to use in the rake file. I cannot determine the correct path to reference both of these files. In fact, this is the ruby issue I'm having the most problem with (require statements/LOAD_PATH) and its quite frustrating; the issue is made worse by the fact that I'm not really sure how to troubleshoot these issues). I am currently avoiding package handles such as bundler, because I need to understand the basics first.

See the attached pic of my code editor which shows project structure (standards gems structure, for a gem called Cart,which is just a dummy app to experiment with).

Cart project structure

So, my rakefile.rb is in the root folder of the project. When specifying a require, where should the path specified be relative to? Is it relative from the file you are requiring from or relative to lib folder or whatever else? I am running rake -T, to list the tasks and these are error messages I see:

Trying to require version.rb file from within Rakefile.rb:

require 'cart/version'

$ rake -T
rake aborted!
cannot load such file -- lib/cart/version
/Users/Sancho/dev/ruby/student/cart/Rakefile.rb:5:in `<top (required)>'

require 'lib/cart/version'

$ rake -T
rake aborted!
cannot load such file -- lib/cart/version
/Users/Sancho/dev/ruby/student/cart/Rakefile.rb:5:in `<top (required)>'
(See full trace by running task with --trace)

Trying to require cart.gemspec from within Rakefile.rb:

require 'cart.gemspec'

$ rake -T
rake aborted!
cannot load such file -- cart.gemspec
/Users/Sancho/dev/ruby/student/cart/Rakefile.rb:6:in `<top (required)>'
(See full trace by running task with --trace)

I am using Ruby version 2.0.0, rake version 0.9.6 (default versions that come with OSX Mavericks).

(PS, my intention is to buy a Ruby IDE like RubyGems, but again, I'm holding off from doing that because I want to learn the basics first, I don't want to become reliant on an IDE fixing issues for my if I don't understand how to fix them from first principles).

Thanks.


Solution

  • When specifying a require, where should the path specified be relative to? Is it relative from the file you are requiring from or relative to lib folder or whatever else?

    require_relative will be relative to the calling file.

    require looks through $LOAD_PATH directories unless the file path is absolute. Since your project does not seem to be asserting itself in $LOAD_PATH and your paths are not absolute it can't find the file.

    Since this seems like a gem have you tried adding the standard

    #this will be the absolute path to lib based on the calling __FILE__
    lib = File.expand_path('../lib', __FILE__)
    #this will include the path in $LOAD_PATH unless it is already included
    $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
    

    This will assert the lib directory in $LOAD_PATH then you can require based on the path from lib e.g. require 'cart/version'