(I’m not using Rails)
I have a rake task that sets the environment that was passed in like:
rake some:task ENV=test
My task is:
desc "environment"
task :environment do
env = ENV['ENV'] || 'development'
end
Now if I have a task like:
desc "t1"
task :t1 [:environment] do
puts env
end
It fails saying env isn’t known, why is that?
I thought the dynamic nature of ruby would do this:
When running the environment task, it would create env variable, and since it is a dependancy that variable would be in scope of my other tasks.
How can I have it so it is in scope?
Note: in case it matters, some of my tasks are in different .rake files, just mentioning if that changes anything.
A Rakefile is just like any other Ruby code. Block variables (variables created inside
do-end) are local to the block.To make your
envvariable visible in other blocks just assign it to an instance variable instead:Or you could use a constant too:
Or create the variable outside the block scope before you use it: