Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capistrano Checking for undefined variable in Task

In Capistrano using the Multi-stage extension I have two environments: prod and testing.

I need a few variables in testing.rb that are not needed in prod.rb and I want some of my tasks to be able to check if the variable is defined and use it if it is, but ignore it if it is not set.

So, in testing.rb I would have something like:

set :foo, 'bar'

prod.rb wouldn't have any reference to :foo since it doesn't need it. In one of my tasks, I would like to do something like:

if defined?(foo)
  # do something with foo
else
  # do something without foo
end

But I keep getting the error:

undefined local variable or method 'foo'

Is there a way to test for undefined global variables in the task? Or do I have to do something like:

set :foo, ''

In all my environments that don't need the :foo variable?

like image 207
Jaymon Avatar asked Feb 07 '11 21:02

Jaymon


1 Answers

Try using exists?(:foo) instead of defined?(foo), as recommended in the Capistrano docs.

like image 177
zetetic Avatar answered Sep 24 '22 06:09

zetetic