2012-09-22 44 views
4

我想重新定義一個寶石的常量動態,所以我不需要修改寶石本身。如何擺脫紅寶石的警告:已經初始化常量

require 'xmlrpc/client' 

XMLRPC::Config.const_set("ENABLE_NIL_PARSER", true) 

warning: already initialized constant ENABLE_NIL_PARSER 

是否可以擺脫警告?

+0

爲什麼你想擺脫它?或者,相反,它是壓制它足夠的,或者你真的不希望它發生? –

+0

可能的重複[http://stackoverflow.com/questions/3375360/how-to-redefine-a-ruby-constant-without-warning](http://stackoverflow.com/questions/3375360/how-to- redefine-a-ruby-constant-without-warning) –

+0

我看到了Josh提到的帖子,但它在我的情況下並不奏效。它抱怨'const_defined?':false不是一個符號(TypeError) – cfpete

回答

6

最簡單的辦法:

v, $VERBOSE = $VERBOSE, nil 
# code goes here 
$VERBOSE = v 
+0

好吧,關閉全局暫時修復它,但我很好奇爲什麼前面提到的解決方案不起作用。謝謝! – cfpete

5

你甚至可以把它包在一個塊中,像這樣的

def ignoring_warnings(&block) 
    begin 
    v, $VERBOSE = $VERBOSE, nil 
    block.call if block 
    ensure 
    $VERBOSE = v 
    end 
end 
相關問題