Warning: refine foo, what!?

I've been doing some unit tests with ruby -W. Wow did I have a lot to
clean up but I still can't get past one warning. I've boiled it down
to this example:

o = Object.new
def o.foo
   'foo'
end
class << o
   undef_method :foo
end
def o.foo
   'foo'
end

# undef_method_test.rb:8: warning: redefine foo

Is my head broken or ruby -W broken? I'm thinking the former.

TIA,
_ugly

"_ugly" <mgarriss@gmail.com> writes:

class << o
   undef_method :foo
end

[...]

# undef_method_test.rb:8: warning: redefine foo

Is my head broken or ruby -W broken? I'm thinking the former.

It's not that simple, because `undef_method' doesn't remove methods;
it overrides them with empty, or ``undefined'' ones.

   void
   rb_undef_method(klass, name)
       VALUE klass;
       const char *name;
   {
       rb_add_method(klass, rb_intern(name), 0, NOEX_UNDEF);
   }

Thanks to that, you can do stuff like this:

   class Foo ; undef_method :dup end
   Foo.new.dup
   NoMethodError: undefined method `dup' for #<Foo:0x402a12b0>

I think you are looking for `remove_method'.

   $ ruby -W
   bar = Object.new
   def bar.foo ; end
   class << bar ; undef_method :foo end
   def bar.foo ; end
   -:4: warning: redefine foo

   $ ruby -W
   bar = Object.new
   def bar.foo ; end
   class << bar ; remove_method :foo end
   def bar.foo ; end

···

--
Daniel Brockman <daniel@brockman.se>

    So really, we all have to ask ourselves:
    Am I waiting for RMS to do this? --TTN.

Do you want your head fixed? :slight_smile:

Mayby it's the way undef_method works. I never looked at the internals but maybe the knowledge is kept somewhere that foo was defined at some point in time.

Kind regards

    robert

···

_ugly <mgarriss@gmail.com> wrote:

I've been doing some unit tests with ruby -W. Wow did I have a lot to
clean up but I still can't get past one warning. I've boiled it down
to this example:

o = Object.new
def o.foo
  'foo'
end
class << o
  undef_method :foo
end
def o.foo
  'foo'
end

# undef_method_test.rb:8: warning: redefine foo

Is my head broken or ruby -W broken? I'm thinking the former.

Hi,

At Mon, 4 Jul 2005 01:35:43 +0900,
_ugly wrote in [ruby-talk:147106]:

Is my head broken or ruby -W broken? I'm thinking the former.

I think it's the latter, because it doesn't appear when
redefining undefined ordinary methods.

Index: eval.c

···

===================================================================
RCS file: /cvs/ruby/src/ruby/eval.c,v
retrieving revision 1.796
diff -U2 -p -r1.796 eval.c
--- eval.c 2 Jul 2005 08:20:25 -0000 1.796
+++ eval.c 4 Jul 2005 02:32:14 -0000
@@ -3914,5 +3914,5 @@ rb_eval(self, n)
         rb_raise(rb_eSecurityError, "redefining method prohibited");
     }
- if (RTEST(ruby_verbose)) {
+ if (body && RTEST(ruby_verbose) && body->nd_cnt == 0 && body->nd_body) {
         rb_warning("redefine %s", rb_id2name(node->nd_mid));
     }

--
Nobu Nakada

ah, thank you very much. see, it was the former.

_ugly