如何访问“全球” (常数?)类的capistrano变量? (红宝石) [英] How to access "global" (constant?) capistrano variables from classes? (ruby)

查看:148
本文介绍了如何访问“全球” (常数?)类的capistrano变量? (红宝石)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 要求'capistrano / capistrano'开始像这样,我猜这是非常正常的: ext / multistage'
require'nokogiri'
require'curb'
require'json'

#覆盖capistrano默认值
set:use_sudo,false
set:normalize_asset_timestamps,false

#我的一些常数
set:my_constant,foo_bar

稍后,我可以在名称空间中的函数或任务中访问常量,如:

  namespace:mycompany do 
def some_function()
运行some_command#{my_constant}
结束

desc <<< -DESC
一些任务描述
DESC
任务:some_task do
运行some_command#{my_constant}
结束
结束

但是,如果我在类中使用常量,如下所示:

  namespace:mycompany do 
class SomeClass
def self.some_static_method()
运行some_command#{my_constant}
结束$ b $结束
结束

失败:

  / config / deploy。 rb:120:在some_static_method中:未定义的局部变量或方法my_constant为#< Class:0x000000026234f8> :: SomeClass(NameError)

我做错了什么?
谢谢

解决方案

deploy.rb文件是instance_evaled,这意味着它在对象的上下文中执行,因此您声明的任何内容都将在您离开该情境之前可用。创建一个提供新上下文的类。



为了访问原始上下文,您必须将对象(self)传递给类方法。 / b>

  namespace:mycompany do 
类SomeClass
def self.some_static_method(cap)
run some_command#{cap.fetch(:my_constant)}
end
end

SomeClass.some_static_method(self)
end

虽然我真的不明白为什么要声明这样的类,但这是一个奇怪的地方。


So my deploy.rb script in capistrano starts like this, which I guess is pretty normal:

require 'capistrano/ext/multistage'
require 'nokogiri'
require 'curb'
require 'json'

# override capistrano defaults
set :use_sudo, false
set :normalize_asset_timestamps, false

# some constant of mine
set :my_constant, "foo_bar"

Later, I can access my constant in functions or tasks within namespaces, like:

namespace :mycompany do
    def some_function()
        run "some_command #{my_constant}"
    end

    desc <<-DESC
        some task description
    DESC
    task :some_task do
        run "some_command #{my_constant}"
    end
end

However, if I use the constant in a class, like this:

namespace :mycompany do
    class SomeClass
        def self.some_static_method()
            run "some_command #{my_constant}"
        end
    end
end

It fails with:

/config/deploy.rb:120:in `some_static_method': undefined local variable or method `my_constant' for #<Class:0x000000026234f8>::SomeClass (NameError)

What am I doing wrong?? Thanks

解决方案

The deploy.rb file is instance_evaled, this means it's being executed inside the context of an object, and as such anything you declare will be available until you leave that context. As soon as you create a class that provides a new context.

In order to access the original context you have to pass the object (self) to the class method.

namespace :mycompany do
  class SomeClass
    def self.some_static_method(cap)
      run "some_command #{cap.fetch(:my_constant)}"
    end
  end

  SomeClass.some_static_method(self)
end

Although I really don't understand why you are declaring a class like this, it's an odd place for it.

这篇关于如何访问“全球” (常数?)类的capistrano变量? (红宝石)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆