I'm not sure why you need to use a lambda then force it to memoize the return. 

Why doesn't this work for you?

set(:user) {Capistrano::CLI.ui.ask("Your SSH User name : ")}

on first use it will ask; then return the same value every time it's used 
thereafter

And on scoping global variables. That's method_missing at work not variable 
scoping.  

If you create a local variable of the same name as a capistrano config 
variable, ruby will use that local variable.  But if there is no local variable 
of the same name ruby tries method_missing defined on the instance; and you are 
in an instance of Capistrano::Configuration when defining tasks; which then 
attempts to find a matching config variable and returns that if found. If not 
ruby keeps walking up the class inheritance tree until it finally tries 
Object#method_missing if that fails then you finally get a no method error

You can still get at a config value if you've defined a local variable of the 
same name. 

set :user, 'foo'

p user # => foo

begin 
  user = 'bar'
  p user # => bar
  p fetch(:user) # => foo
end

p user # => foo



On Nov 15, 2011, at 2:54 PM, Craig White <[email protected]> wrote:

> On Nov 15, 2011, at 1:42 PM, Craig White wrote:
> 
>> 
>> On Nov 15, 2011, at 10:56 AM, Lee Hambley wrote:
>> 
>>> I meant, by "written long form" that it wasn't valid syntax… but it was 
>>> supposed to be easier to understand:
>>> 
>>> set(:branch, lambda { .... })
>>> 
>>> is the required way...
>> ----
>> something about capistrano makes global variables local and makes me crazy...
>> 
>> set(:user, lambda do
>> if $my_user == nil then
>>   $my_user = Capistrano::CLI.ui.ask("Your SSH User name : ")
>> else
>>   $my_user
>> end
>> end)
>> 
>> and if any 'task' has multiple deploys or invoked before/after tasks that 
>> call :user a second time, I am still asked for the user name a second time.
> ----
> same result as a one-liner too...  ;-(
> 
> set(:user, lambda { $my_user == nil ? $my_user = Capistrano::CLI.ui.ask("Your 
> SSH User name : ") : $my_user } )
> 
> Craig
> 
> -- 
> * You received this message because you are subscribed to the Google Groups 
> "Capistrano" group.
> * To post to this group, send email to [email protected]
> * To unsubscribe from this group, send email to 
> [email protected] For more options, visit this group at 
> http://groups.google.com/group/capistrano?hl=en

-- 
* You received this message because you are subscribed to the Google Groups 
"Capistrano" group.
* To post to this group, send email to [email protected]
* To unsubscribe from this group, send email to 
[email protected] For more options, visit this group at 
http://groups.google.com/group/capistrano?hl=en

Reply via email to