Project

General

Profile

Feature #6284 ยป 0003-proc.c-Support-any-callable-when-composing-Procs.patch

mudge (Paul Mucur), 12/30/2015 12:28 PM

View differences:

proc.c
2824 2824
    VALUE f, g, fargs;
2825 2825
    f = RARRAY_AREF(args, 0);
2826 2826
    g = RARRAY_AREF(args, 1);
2827
    fargs = rb_ary_new3(1, rb_proc_call_with_block(g, argc, argv, passed_proc));
2827
    fargs = rb_ary_new3(1, rb_funcall_with_block(g, idCall, argc, argv, passed_proc));
2828 2828

  
2829 2829
    return rb_proc_call(f, fargs);
2830 2830
}
......
2833 2833
  *  call-seq:
2834 2834
  *     prc * g -> a_proc
2835 2835
  *
2836
  *  Returns a proc that is the composition of this proc and the given proc <i>g</i>.
2836
  *  Returns a proc that is the composition of this proc and the given <i>g</i>.
2837 2837
  *  The returned proc takes a variable number of arguments, calls <i>g</i> with them
2838 2838
  *  then calls this proc with the result.
2839 2839
  *
......
2849 2849
    rb_proc_t *procp;
2850 2850
    int is_lambda;
2851 2851

  
2852
    if (!rb_obj_is_method(g) && !rb_obj_is_proc(g)) {
2853
        rb_raise(rb_eTypeError,
2854
                "wrong argument type %s (expected Proc/Method)",
2855
                rb_obj_classname(g));
2856
    }
2857

  
2858
    if (rb_obj_is_method(g)) {
2859
        g = method_to_proc(g);
2860
    }
2861

  
2862 2852
    args = rb_ary_new3(2, self, g);
2863 2853

  
2864 2854
    GetProcPtr(self, procp);
......
2875 2865
  *  call-seq:
2876 2866
  *     meth * g -> a_proc
2877 2867
  *
2878
  *  Returns a proc that is the composition of this method and the given proc <i>g</i>.
2868
  *  Returns a proc that is the composition of this method and the given <i>g</i>.
2879 2869
  *  The returned proc takes a variable number of arguments, calls <i>g</i> with them
2880 2870
  *  then calls this method with the result.
2881 2871
  *
test/ruby/test_method.rb
977 977
    assert_equal(6, h.call(2))
978 978
  end
979 979

  
980
  def test_compose_with_nonproc_or_method
980
  def test_compose_with_callable
981
    c = Class.new {
982
      def f(x) x * 2 end
983
    }
984
    c2 = Class.new {
985
      def call(x) x + 1 end
986
    }
987
    f = c.new.method(:f)
988
    g = f * c2.new
989

  
990
    assert_equal(6, g.call(2))
991
  end
992

  
993
  def test_compose_with_noncallable
981 994
    c = Class.new {
982 995
      def f(x) x * 2 end
983 996
    }
984 997
    f = c.new.method(:f)
998
    g = f * 5
985 999

  
986
    assert_raise(TypeError) {
987
      f * 5
1000
    assert_raise(NoMethodError) {
1001
      g.call(2)
988 1002
    }
989 1003
  end
990 1004
end
test/ruby/test_proc.rb
1385 1385
    assert_equal(6, h.call(2))
1386 1386
  end
1387 1387

  
1388
  def test_compose_with_nonproc_or_method
1388
  def test_compose_with_callable
1389 1389
    f = proc{|x| x * 2}
1390
    c = Class.new {
1391
      def call(x) x + 1 end
1392
    }
1393
    g = f * c.new
1394

  
1395
    assert_equal(6, g.call(2))
1396
  end
1397

  
1398
  def test_compose_with_noncallable
1399
    f = proc{|x| x * 2}
1400
    g = f * 5
1390 1401

  
1391
    assert_raise(TypeError) {
1392
      f * 5
1402
    assert_raise(NoMethodError) {
1403
      g.call(2)
1393 1404
    }
1394 1405
  end
1395 1406
end
1396
-