ruby-changes:49902
From: nobu <ko1@a...>
Date: Wed, 24 Jan 2018 15:25:08 +0900 (JST)
Subject: [ruby-changes:49902] nobu:r62020 (trunk): vm_args.c: to_proc refinements
nobu 2018-01-24 15:25:02 +0900 (Wed, 24 Jan 2018) New Revision: 62020 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62020 Log: vm_args.c: to_proc refinements * vm_args.c (vm_to_proc): enable #to_proc by refinements at Proc passing as a block. patched by osyo (manga osyo). [Feature #14223] Modified files: trunk/test/ruby/test_refinement.rb trunk/vm_args.c Index: vm_args.c =================================================================== --- vm_args.c (revision 62019) +++ vm_args.c (revision 62020) @@ -792,7 +792,16 @@ vm_to_proc(VALUE proc) https://github.com/ruby/ruby/blob/trunk/vm_args.c#L792 { if (UNLIKELY(!rb_obj_is_proc(proc))) { VALUE b; - b = rb_check_convert_type_with_id(proc, T_DATA, "Proc", idTo_proc); + const rb_callable_method_entry_t *me = + rb_callable_method_entry_with_refinements(CLASS_OF(proc), idTo_proc, NULL); + + if (me) { + b = vm_call0(GET_EC(), proc, idTo_proc, 0, NULL, me); + } + else { + /* NOTE: calling method_missing */ + b = rb_check_convert_type_with_id(proc, T_DATA, "Proc", idTo_proc); + } if (NIL_P(b) || !rb_obj_is_proc(b)) { rb_raise(rb_eTypeError, Index: test/ruby/test_refinement.rb =================================================================== --- test/ruby/test_refinement.rb (revision 62019) +++ test/ruby/test_refinement.rb (revision 62020) @@ -2010,6 +2010,122 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L2010 assert_equal(:foo, ToSymbol.new("foo").symbol) end + module ToProc + def self.call &block + block.call + end + + class ReturnProc + c = self + using Module.new { + refine c do + def to_proc + proc { "to_proc" } + end + end + } + def call + ToProc.call &self + end + end + + class ReturnNoProc + c = self + using Module.new { + refine c do + def to_proc + true + end + end + } + + def call + ToProc.call &self + end + end + + class PrivateToProc + c = self + using Module.new { + refine c do + private + def to_proc + proc { "private_to_proc" } + end + end + } + + def call + ToProc.call &self + end + end + + + class NonProc + def call + ToProc.call &self + end + end + + class MethodMissing + def method_missing *args + proc { "method_missing" } + end + + def call + ToProc.call &self + end + end + + class ToProcAndMethodMissing + def method_missing *args + proc { "method_missing" } + end + + c = self + using Module.new { + refine c do + def to_proc + proc { "to_proc" } + end + end + } + + def call + ToProc.call &self + end + end + + class ToProcAndRefinements + def to_proc + proc { "to_proc" } + end + + c = self + using Module.new { + refine c do + def to_proc + proc { "refinements_to_proc" } + end + end + } + + def call + ToProc.call &self + end + end + end + + def test_to_proc + assert_equal("to_proc", ToProc::ReturnProc.new.call) + assert_equal("private_to_proc", ToProc::PrivateToProc.new.call) + assert_raise(TypeError){ ToProc::ReturnNoProc.new.call } + assert_raise(TypeError){ ToProc::NonProc.new.call } + assert_equal("method_missing", ToProc::MethodMissing.new.call) + assert_equal("to_proc", ToProc::ToProcAndMethodMissing.new.call) + assert_equal("refinements_to_proc", ToProc::ToProcAndRefinements.new.call) + end + def test_unused_refinement_for_module bug14068 = '[ruby-core:83613] [Bug #14068]' assert_in_out_err([], <<-INPUT, ["M1#foo"], [], bug14068) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/