Rake and Constants

Here’s a gotcha that I came across today: don’t define a constant as something in one Rails rake task file, and then define the same constant as something different in another file, because the last constant will supercede them all:

a.rake:
DATA = ‘Hi there!’

desc “Test A”
task :testa do
  puts DATA
end

b.rake:
DATA = 'Hallo!’

desc “Test B”
task :test
b do
  puts DATA
end

$ rake testa
Hallo!

$ rake test
b
Hallo!

I guess this happens because Rake is loading all of the task files before running the requested task. There’s probably a way to namespace the constant, but I need to do more research on that. In the meantime I’ll use different constant names!