Programmatically disabling warnings

S

Sean Russell

I'm trying a new method of doing encoding support in REXML, and one of
the things I'm trying is redefining a couple of methods based on the
encoding. The problem is that if "-w" is enabled on the interpreter,
Ruby spews out "discarding old method definitions" every time this
happens, which is certainly not what I want.

Obviously, since this is a library, I have no control over whether the
application enables "-w". Is it possible to programmatically disable
the warning? I've played a bit with undef'ing the methods before
redefining them, but had no success with that.

Any suggestions would be greatly appreciated.

--- SER
 
M

Mike Stok

I'm trying a new method of doing encoding support in REXML, and one of
the things I'm trying is redefining a couple of methods based on the
encoding. The problem is that if "-w" is enabled on the interpreter,
Ruby spews out "discarding old method definitions" every time this
happens, which is certainly not what I want.

Obviously, since this is a library, I have no control over whether the
application enables "-w". Is it possible to programmatically disable
the warning? I've played a bit with undef'ing the methods before
redefining them, but had no success with that.

Any suggestions would be greatly appreciated.

Can you temporatily unset $VERBOSE, do your redefinition, then restore
$VERBOSE?

Mike
 
C

Christoph

Sean Russell said:
I'm trying a new method of doing encoding support in REXML, and one of
the things I'm trying is redefining a couple of methods based on the
encoding. The problem is that if "-w" is enabled on the interpreter,
Ruby spews out "discarding old method definitions" every time this
happens, which is certainly not what I want.

Obviously, since this is a library, I have no control over whether the
application enables "-w". Is it possible to programmatically disable
the warning? I've played a bit with undef'ing the methods before
redefining them, but had no success with that.

Any suggestions would be greatly appreciated.

I can think of two ways - change $VERBOSE or invoke
Module::remove_method (Matz used the trick to fix a method
redefinition warning in singleton.rb) before redefining your
method. The problem is that both techniques are a priori not
thread-safe - in fact the cvs version of singleton.rb still contains
this bug.

If you a lot these method redefinitions you probably
should write yourself a threadsafe-wrapper (ultimately
there is the question if a method redefinition should be
issued at all. One solution might be an even finer grained
warning level system ...)


---
class Module
private
def suppress_verbose
unless restore_critical = Thread.critical
Thread.critical = true
end
restore_verbose = $VERBOSE
$VERBOSE = nil
yield
ensure
$VERBOSE = restore_verbose
Thread.critical = restore_critical
end

def pre_remove_method(sym)
unless restore_critical = Thread.critical
Thread.critical = true
end
remove_method sym
yield
ensure
Thread.critical = restore_critical
end
end


$VERBOSE = true

class A
def foo
end
def bar
end
suppress_verbose {
def foo
p "foo"
end
}
pre_remove_method:)bar) {
def bar
p "bar"
end
}
end

A.new.foo
A.new.bar
 
G

Greg McIntyre

Christoph said:
I can think of two ways - change $VERBOSE or invoke
Module::remove_method (Matz used the trick to fix a method
redefinition warning in singleton.rb) before redefining your
method. The problem is that both techniques are a priori not
thread-safe - in fact the cvs version of singleton.rb still contains
this bug.

And you can also use the alias keyword:

class A
def hello
puts "Hello"
end
end

class A
alias old_hello hello
def hello # no warning
puts "World"
end
end

A.new.hello # prints "World"
 
S

Sean Russell

Can you temporatily unset $VERBOSE, do your redefinition, then restore
$VERBOSE?

Thanks for the responses, both for the $VERBOSE and
Module#remove_method. I should have seen remove_method, but I have a
bugger of a time finding global variable descriptions in the Axe book.
Which begs another question: is there a good, centralized place where
are these variables are described? Every time I need some rarely used
global, it seems like it takes me an hour to locate it. ("Was it
FNAME, or FILENAME?").

I'm not terribly concerned with thread safety, since -- worst case --
you either do or don't get a warning. Since this only happens during
the changing of the encoding of a document, and I expect this to be
fairly rare, I'm even less concerned about it.

I'm not using the alias trick (which I've used before) because this
redefinition happens in several files, and I don't want to add an
alias call to each of them. I'd prefer to turn off verbosity for a
very short time, do a "load", and then restore its state.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top