Tim Pease wrote:
> Is there a way to disable specific warnings from within a Ruby program
> (not from the command line)?
>
> The warnings are coming from some meta-programming code that is
> redefining instance methods on the fly based on user input. So maybe a
> better question would be, is there a way to redefine methods without
> causing Ruby to spew out a warning each time the method is redefined.

Sure - redefine Kernel#warn. ;)

Seriously, I think what we need are structured warnings.  I brought
this up way back in 2002: http://tinyurl.com/yfko6z

I'm certainly not the first to think of it (Perl lets you disable
specific kinds of warnings, it's been brought up on IRC several times
over the years), but some sort of structured warning that programmers
could specifically raise and/or ignore is something I've wanted for a
long time.

I would think it would just be a matter of copying exception.c, ripping
out the longjmp/exit parts of it, defining a warning hierarchy, and
adding a Warning.ignore class method.  Of course, it probably isn't
that simple.

Any, I imagine instead of doing this within your code:

warn, "redefinition of constant FOO"

Internally, you would do this:

raise RedefinedWarning, "redefinition of constant FOO"

And then, a programmer could do this:

RedefinedWarning.ignore # ignore all redefintion warnings

And, I think Sean originally suggested allowing a block syntax, so you
could temporarily ignore warnings:

RedefinedWarning.ignore do
   # Ignore redefinition warnings within this block only
end

Back to your question, though, I suppose we would have add a
Warning.disable in order to prevent the warning from ever being
generated in the first place:

RedefinedWarning.disable do
   def some_method
      # Pretend you didn't notice that I've redefined this method
   end
end

Whether that's good practice or not, I leave to the viewing audience.

Probably a much longer answer than you wanted, but there you go.

Regards,

Dan