From 31ef3124a9db534abcc3e323f5d3cb696eda3bf5 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 22 Feb 2017 02:02:11 +0000 Subject: numeric.c: Numeric#clone and #dup * numeric.c (num_clone, num_dup): no longer raises TypeError, returns the receiver instead as well as Integer and Float. [ruby-core:79636] [Bug #13237] * object.c (rb_immutable_obj_clone): immutable object clone with freeze optional keyword argument. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57682 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- numeric.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'numeric.c') diff --git a/numeric.c b/numeric.c index 1a685ed252..65edfec205 100644 --- a/numeric.c +++ b/numeric.c @@ -552,18 +552,38 @@ num_sadded(VALUE x, VALUE name) UNREACHABLE; } +#if 0 /* - * Numerics are immutable values, which should not be copied. + * call-seq: + * num.clone(freeze: true) -> num * - * Any attempt to use this method on a Numeric will raise a TypeError. + * Returns the receiver. + * _freeze_ cannot be +false+. */ static VALUE -num_init_copy(VALUE x, VALUE y) +num_clone(int argc, VALUE *argv, VALUE x) { - rb_raise(rb_eTypeError, "can't copy %"PRIsVALUE, rb_obj_class(x)); + return rb_immutable_obj_clone(argc, argv, x); +} +#else +# define num_clone rb_immutable_obj_clone +#endif - UNREACHABLE; +#if 0 +/* + * call-seq: + * num.dup -> num + * + * Returns the receiver. + */ +static VALUE +num_dup(VALUE x) +{ + return x; } +#else +# define num_dup num_uplus +#endif /* * call-seq: @@ -5222,8 +5242,9 @@ Init_Numeric(void) rb_define_method(rb_cNumeric, "singleton_method_added", num_sadded, 1); rb_include_module(rb_cNumeric, rb_mComparable); - rb_define_method(rb_cNumeric, "initialize_copy", num_init_copy, 1); rb_define_method(rb_cNumeric, "coerce", num_coerce, 1); + rb_define_method(rb_cNumeric, "clone", num_clone, -1); + rb_define_method(rb_cNumeric, "dup", num_dup, 0); rb_define_method(rb_cNumeric, "i", num_imaginary, 0); rb_define_method(rb_cNumeric, "+@", num_uplus, 0); -- cgit v1.2.3