2

I understand that Rake is a DSL and a subset of Ruby. I realize I have to create Rakefile as the entrypoint (surprisingly Rakefile.rake is not an option). In this rakefile I may include references to other *.rake, *.rb files:

load './file1.rake' 
#require './file1.rake' #Why does this fail?
require './file2' 

task 'a' => ['rakeFile', 'rbFile'] do 
    puts 'helo wurld'
end


task 'b' => ['rbFile'] do 
    puts 'helo wurld'
end

require seems to be unable to handle *.rake files. This seems problematic. I don't understand why I should use *.rake file extensions. When programming in Rake, I can't use require with *.rake, and *.rb seems to work just fine...what benefit does using *.rake have over *.rb?

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348

3 Answers3

2

I use ".rb" for the extension for both Ruby and Rake files. Rake files ARE Ruby files, they just refer to extensions provided by the DSL. By using the same extension my editors are happy and Ruby is happy.

Ruby's require will automatically provide the extension if you don't:

If the filename has the extension “.rb”, it is loaded as a source file; if the extension is “.so”, “.o”, or “.dll”, or the default shared library extension on the current platform, Ruby loads the shared library as a Ruby extension. Otherwise, Ruby tries adding “.rb”, “.so”, and so on to the name until found. If the file named cannot be found, a LoadError will be raised.

Also, note that load does NOT do the same thing that require does. load forces the file to reload, where require does it once. That means that it can be more costly if you load the file in a loop, especially if that file has a slow startup.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 3
    **"Ruby's `require` will automatically provide the extension if you don't:"**. As a note even if you provide an extension to `require` such as the OP did using `require ./file1.rake` ruby does not care unless it has one of the specified extensions `rb,so,o,dll`. MRI actually uses a `if..else if` statement to determine if it is one of these file types and if not it tries to impose these extensions in order. – engineersmnky Feb 03 '15 at 17:25
  • What I get from this answer is that they are both the same...except `require` doesn't work. Then, I will also use *.rb as you do. – P.Brian.Mackey Feb 03 '15 at 17:52
0

Whether you're using the .rake or .rb file extension, those files are getting loaded in as ruby. The only extra advantage the .rake file extension provides is that the rake program understands that files with extension .rake definitely contain rake tasks, and so it will find them in there (for example rake -T, which lists available tasks, will find rake tasks in .rake files)

Alexis Andersen
  • 785
  • 6
  • 12
  • Hm. rake -T works with both *.rb and *.rake files...it even works when you have no file extension. I don't see any differences here. – P.Brian.Mackey Feb 03 '15 at 17:51
  • Currently, `rake -T` / `rake --tasks` does no longer work with `*.rb` files, but only `*.rake`-files it seems. – Qqwy Jun 07 '19 at 12:59
0

There is at least one difference in the behavior of rake and ruby. gets.chomp must be written as STDIN.gets.chomp.

Evidently, Rails uses *.rake as a convention when storing files in lib/tasks. Since my project is .NET I don't really care about that.

Community
  • 1
  • 1
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348