warning: refine foo, what!?

U

_ugly

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
 
D

Daniel Brockman

_ugly said:
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
 
R

Robert Klemme

_ugly said:
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.

Do you want your head fixed? :)

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
 
N

nobuyoshi nakada

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));
}
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top