# Redifition warning for module\_function

**URL:** <https://www.ruby-forum.com/t/redifition-warning-for-module-function/176325>\
**Category:** Development\
**Created:** [September 29, 2009, 5:51am UTC](https://www.ruby-forum.com/t/redifition-warning-for-module-function/176325 "2009-09-29T05:51:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9c0ed/32.png) [@nobu](https://www.ruby-forum.com/u/nobu)\
**Post date:** [September 29, 2009, 5:51am UTC](https://www.ruby-forum.com/t/redifition-warning-for-module-function/176325/1 "2009-09-29T05:51:19Z")

</div>

e$B$J$+$@$G$9!#e(B

jsone$B$G7Y9p$,=P$^$9!#e(B

$ ruby -w -rjson -ep  
/usr/local/lib/ruby/1.9.1/json/common.rb:273: warning: method redefined;  
discarding old recurse\_proc

e$B:G\>.2=$9$k$He(B

$ ruby -w -e ‘module M; module\_function; def foo;end; module\_function  
:foo; end’  
-e:1: warning: method redefined; discarding old foo

module\_functione$B$K$h$C$FDj5A$5$l$?FC0[%a%=%C%I$r$b$&0lEYe(B  
module\_functione$B$G:FDj5A$H$$$&$3$H$N$h$&$G$9!#\<B:]$K$OF1$8e(Baliase$B$Ne(B  
e$B$h$&$J$b$N$r:n$jD\>$9$@$1$J$N$G!"$3$N7Y9p$OITMW$G$O$J$$$+$H;W$$$^$9!#e(B

# Index: method.h

— method.h (revision 25143)  
+++ method.h (working copy)  
@@ -75,4 +75,5 @@ typedef struct rb\_method\_entry\_struct {

#define UNDEFINED\_METHOD\_ENTRY\_P(me) (!(me) || !(me)-\>def ||  
(me)-\>def-\>type == VM\_METHOD\_TYPE\_UNDEF)  
+#define CFUNC\_EQUAL(cf1, cf2) ((cf1).func == (cf2).func && (cf1).argc  
== (cf2).argc)

# void rb\_add\_method\_cfunc(VALUE klass, ID mid, VALUE (\*func)(ANYARGS), int argc, rb\_method\_flag\_t noex); Index: vm\_method.c

— vm\_method.c (revision 25143)  
+++ vm\_method.c (working copy)  
@@ -142,4 +142,34 @@ rb\_free\_method\_entry(rb\_method\_entry\_t \*  
}

+static inline int  
+method\_definition\_equal(const rb\_method\_definition\_t \*def1, const  
rb\_method\_definition\_t \*def2)  
+{

- if (def1 == def2) return TRUE;
- if (!def1 || !def2) return FALSE;
- if (def1-\>type != def2-\>type) return FALSE;
- if (def1-\>original\_id != def2-\>original\_id) return FALSE;
- switch (def1-\>type) {
- 

```
 case VM_METHOD_TYPE_CFUNC:

```

- if (!CFUNC\_EQUAL(def1-\>body.cfunc, def2-\>body.cfunc)) return FALSE;
- break;
- 

```
 case VM_METHOD_TYPE_IVAR:

```

- 

```
 case VM_METHOD_TYPE_ATTRSET:

```

- if (def1-\>body.attr\_id != def2-\>body.attr\_id) return FALSE;
- break;
- 

```
 case VM_METHOD_TYPE_BMETHOD:

```

- if (def1-\>body.proc != def2-\>body.proc) return FALSE;
- break;
- 

```
 case VM_METHOD_TYPE_ISEQ:

```

- if (def1-\>body.iseq != def2-\>body.iseq) return FALSE;
- break;
- 

```
 case VM_METHOD_TYPE_OPTIMIZED:

```

- if (def1-\>body.optimize\_type != def2-\>body.optimize\_type) return  
FALSE;
- break;
- 

```
 default:

```

- return FALSE;
- }
- return TRUE;  
+}
- 

static rb\_method\_entry\_t \*  
rb\_add\_method\_def(VALUE klass, ID mid, rb\_method\_type\_t type,  
rb\_method\_definition\_t \*def, rb\_method\_flag\_t noex)  
@@ -180,5 +210,5 @@ rb\_add\_method\_def(VALUE klass, ID mid, r  
rb\_method\_definition\_t \*old\_def = old\_me-\>def;

- if (old\_def == def) return old\_me;

- if (method\_definition\_equal(old\_def, def)) return old\_me;  
rb\_vm\_check\_redefinition\_opt\_method(old\_me);

# Index: test/ruby/test\_class.rb

— test/ruby/test\_class.rb (revision 25143)  
+++ test/ruby/test\_class.rb (working copy)  
@@ -99,4 +99,15 @@ class TestClass \< Test::Unit::TestCase  
end

- def verbose\_warning
- class \<\< (stderr = “”)
- 

```
 alias write <<

```

- end
- stderr, $stderr, verbose, $VERBOSE = $stderr, stderr, $VERBOSE,  
true
- yield
- ensure
- stderr, $stderr, $VERBOSE = $stderr, stderr, verbose
- return stderr
- end
- def test\_module\_function  
c = Class.new  
@@ -104,4 +115,27 @@ class TestClass \< Test::Unit::TestCase  
Module.instance\_method(:module\_function).bind©.call(:foo)  
end
- stderr = verbose\_warning do
- 

```
 Module.new do

```

- 

```
   def foo; end

```

- 

```
   def foo; end

```

- 

```
 end

```

- end
- assert\_match(/method redefined; discarding old foo/, stderr)
- stderr = verbose\_warning do
- 

```
 Module.new do

```

- 

```
   def foo; end

```

- 

```
   alias bar foo

```

- 

```
   alias bar foo

```

- 

```
 end

```

- end
- assert\_equal("", stderr)
- stderr = verbose\_warning do
- 

```
 Module.new do

```

- 

```
   module_function

```

- 

```
   def foo; end

```

- 

```
   module_function :foo

```

- 

```
 end

```

- end
- assert\_equal("", stderr)  
end

---

<div class="post-metadata">

**Author:** ![nobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9c0ed/32.png) [@nobu](https://www.ruby-forum.com/u/nobu)\
**Post date:** [September 29, 2009, 6:22am UTC](https://www.ruby-forum.com/t/redifition-warning-for-module-function/176325/2 "2009-09-29T06:22:56Z")

</div>

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:39397] redifition warning for module\_function”  
on Tue, 29 Sep 2009 12:50:26 +0900, Nobuyoshi N.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|jsone$B$G7Y9p$,=P$^$9!#e(B

|module\_functione$B$K$h$C$FDj5A$5$l$?FC0[%a%=%C%I$r$b$&0lEYe(B  
|module\_functione$B$G:FDj5A$H$$$&$3$H$N$h$&$G$9!#\<B:]$K$OF1$8e(Baliase$B$Ne(B  
|e$B$h$&$J$b$N$r:n$jD\>$9$@$1$J$N$G!"$3$N7Y9p$OITMW$G$O$J$$$+$H;W$$$^$9!#e(B

e$B$^$“!”$“$($F6hJL$7$J$+$C$?$@$1$J$N$G!”%Q%C%A$rEv$F$F$b$h$$$se(B  
e$B$8$c$J$$$G$7$g$&$+!#e(B
