4

I'm trying to redefine a gem's constant dynamically so I don't need to modify the gem itself.

require 'xmlrpc/client'

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

warning: already initialized constant ENABLE_NIL_PARSER

Is it possible to get rid of the warning?

cfpete
  • 4,143
  • 8
  • 27
  • 23
  • Why do you want to get rid of it? Or, rather, is suppressing it sufficient or do you actually wish to not have it occur? – Andrew Marshall Sep 22 '12 at 02:16
  • Possible duplicate of [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) – Josh Voigts Sep 22 '12 at 02:54
  • I saw the post mentioned by Josh already but it didn't work in my case. It complained about `const_defined?': false is not a symbol (TypeError) – cfpete Sep 22 '12 at 06:16

2 Answers2

6

The easy way:

v, $VERBOSE = $VERBOSE, nil
# code goes here
$VERBOSE = v
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • Okay, turning off the global temporarily fixed it, but I'm curious as to why the previous mentioned solution didn't work. Thanks! – cfpete Sep 22 '12 at 16:04
5

You may even wrap it in a block, like this

def ignoring_warnings(&block)
  begin
    v, $VERBOSE = $VERBOSE, nil
    block.call if block
  ensure
    $VERBOSE = v
  end
end
Pietro Di Bello
  • 492
  • 4
  • 10