summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-08-07 09:01:33 -0700
committerJeremy Evans <code@jeremyevans.net>2019-09-21 16:10:37 -0700
commit2e551356a7a6e74ba07283e000ff16f5d1ea6506 (patch)
tree99c123bb12c43346d9b1748cd73c42b883c6bb0a /ext
parent7470f965650bf17875632f0c5f9e5a4d9de9fc3f (diff)
Make Kernel#{Pathname,BigDecimal,Complex} return argument if given correct type
This is how Kernel#{Array,String,Float,Integer,Hash,Rational} work. BigDecimal and Complex instances are always frozen, so this should not cause backwards compatibility issues for those. Pathname instances are not frozen, so potentially this could cause backwards compatibility issues by not returning a new object. Based on a patch from Joshua Ballanco, some minor changes by me. Fixes [Bug #7522]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2473
Diffstat (limited to 'ext')
-rw-r--r--ext/bigdecimal/bigdecimal.c3
-rw-r--r--ext/pathname/pathname.c2
2 files changed, 5 insertions, 0 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 652f341fb9..6d5047553b 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -2722,6 +2722,9 @@ f_BigDecimal(int argc, VALUE *argv, VALUE self)
Real *pv;
VALUE obj;
+ if (argc > 0 && CLASS_OF(argv[0]) == rb_cBigDecimal) {
+ if (argc == 1 || (argc == 2 && RB_TYPE_P(argv[1], T_HASH))) return argv[0];
+ }
obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, 0);
pv = BigDecimal_new(argc, argv);
if (pv == NULL) return Qnil;
diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c
index 70f82583a1..1cf0f90b9f 100644
--- a/ext/pathname/pathname.c
+++ b/ext/pathname/pathname.c
@@ -1323,6 +1323,8 @@ path_unlink(VALUE self)
static VALUE
path_f_pathname(VALUE self, VALUE str)
{
+ if (CLASS_OF(str) == rb_cPathname)
+ return str;
return rb_class_new_instance(1, &str, rb_cPathname);
}