Rake variable in namespace

Today I see a strange thing in my rake script. I have two Rake tasks under different namespaces, for example:

path = "/home/tomcat/tomcat"

namespace :stage do
  path = "/home/tomcat/stage-tomcat"
  desc "Deploys a java application to stage tomcat"
  task :java_deploy do
    puts path # stage:java_deploy should print /home/tomcat/stage-tomcat
  end
end

namespace :production do
  path = "/home/tomcat/production-tomcat"
  desc "Deploys a java application to production tomcat"
  task :java_deploy do
    puts path # production:java_deploy should print /home/tomcat/production-tomcat
  end
end

When I run: rake stage:java_deployit prints

/ main / cat / production-cat

I was expecting / home / tomcat / stage-tomcat . If I delete the first line path = "/home/tomcat/tomcat"from the rake file, it works as expected.

Any idea why this kolavari? :)

Thanks in advance!

+4
source share
1 answer

This does not apply to Rake, it is simply a consequence of the lexical scope and how Ruby handles local variables, declaring them the first time they are used.

First you assign a value path:

path = "/home/tomcat/tomcat"

stage :

path = "/home/tomcat/stage-tomcat"

, , - .

java_deploy, . path, , .

, production . , :

path = "/home/tomcat/production-tomcat"

, path, , /home/tomcat/production-tomcat.

path, . , path ( ) .

+2

All Articles