[前][次][番号順一覧][スレッド一覧]

ruby-changes:41140

From: ko1 <ko1@a...>
Date: Sun, 20 Dec 2015 11:15:04 +0900 (JST)
Subject: [ruby-changes:41140] ko1:r53213 (trunk): * proc.c (rb_mod_define_method): should check Symbol or not.

ko1	2015-12-20 11:14:57 +0900 (Sun, 20 Dec 2015)

  New Revision: 53213

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53213

  Log:
    * proc.c (rb_mod_define_method): should check Symbol or not.
      [Bug #11850]
    
    * test/ruby/test_method.rb: add a test.

  Modified files:
    trunk/ChangeLog
    trunk/proc.c
    trunk/test/ruby/test_method.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53212)
+++ ChangeLog	(revision 53213)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Dec 20 11:14:11 2015  Koichi Sasada  <ko1@a...>
+
+	* proc.c (rb_mod_define_method): should check Symbol or not.
+	  [Bug #11850]
+
+	* test/ruby/test_method.rb: add a test.
+
 Sun Dec 20 11:01:57 2015  Koichi Sasada  <ko1@a...>
 
 	* proc.c (rb_mod_define_method): fix notation.
Index: proc.c
===================================================================
--- proc.c	(revision 53212)
+++ proc.c	(revision 53213)
@@ -1774,8 +1774,13 @@ rb_mod_define_method(int argc, VALUE *ar https://github.com/ruby/ruby/blob/trunk/proc.c#L1774
 	rb_thread_t *th = GET_THREAD();
 	rb_block_t *block = rb_vm_control_frame_block_ptr(th->cfp);
 	if (!block) rb_raise(rb_eArgError, proc_without_block);
+
 	body = block->proc;
-	if (!body) {
+
+	if (SYMBOL_P(body)) {
+	    body = rb_sym_to_proc(body);
+	}
+	else if (!body) {
 	    body = rb_vm_make_proc_lambda(th, block, rb_cProc, TRUE);
 	}
 #endif
Index: test/ruby/test_method.rb
===================================================================
--- test/ruby/test_method.rb	(revision 53212)
+++ test/ruby/test_method.rb	(revision 53213)
@@ -939,4 +939,18 @@ class TestMethod < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_method.rb#L939
     assert_equal([:m1, :m2, :m3].sort, MethodInMethodClass.public_instance_methods(false).sort)
     assert_equal([].sort, MethodInMethodClass.private_instance_methods(false).sort)
   end
+
+  def test_define_method_with_symbol
+    assert_normal_exit %q{
+      define_method(:foo, &:to_s)
+      define_method(:bar, :to_s.to_proc)
+    }, '[Bug #11850]'
+    c = Class.new{
+      define_method(:foo, &:to_s)
+      define_method(:bar, :to_s.to_proc)
+    }
+    obj = c.new
+    assert_equal('1', obj.foo(1))
+    assert_equal('1', obj.bar(1))
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]