[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Programmatically disabling warnings

ser

10/9/2003 5:42:00 PM

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
4 Answers

mike

10/9/2003 6:39:00 PM

0

In article <83173408.0310090941.1f3a3502@posting.google.com>,
Sean Russell <ser@germane-software.com> wrote:
>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

--
mike@stok.co.uk | The "`Stok' disclaimers" apply.
http://www.stok.co... | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exe... | 75D2 9EC4 C1C0 0599 13DA

Christoph

10/9/2003 9:15:00 PM

0


"Sean Russell" <ser@germane-software.com> wrote in message
news:83173408.0310090941.1f3a3502@posting.google.com...
> 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
---


/Christoph


Greg McIntyre

10/9/2003 10:52:00 PM

0

"Christoph" <swap(news_chr)@gmx.net> wrote:
> 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"

--
Greg McIntyre ======[ greg@puyo.cjb.net ]===[ http://pu... ]===

ser

10/10/2003 2:57:00 AM

0

mike@ratdog.stok.co.uk (Mike Stok) wrote in message news:<8Xhhb.157332$3r1.126952@news02.bloor.is.net.cable.rogers.com>...
> 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.