diff --git a/complex.c b/complex.c index 2efe6a4..46b9488 100644 --- a/complex.c +++ b/complex.c @@ -487,6 +487,8 @@ f_complex_new2(VALUE klass, VALUE x, VALUE y) static VALUE nucomp_f_complex(int argc, VALUE *argv, VALUE klass) { + if (argc == 1 && CLASS_OF(argv[0]) == rb_cComplex) + return argv[0]; return rb_funcall2(rb_cComplex, id_convert, argc, argv); } diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 1435c03..6e06b94 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -2409,7 +2409,12 @@ BigDecimal_new(int argc, VALUE *argv) static VALUE BigDecimal_global_new(int argc, VALUE *argv, VALUE self) { - Real *pv = BigDecimal_new(argc, argv); + Real *pv; + + if (argc == 1 && CLASS_OF(argv[0]) == rb_cBigDecimal) + return argv[0]; + + pv = BigDecimal_new(argc, argv); if (ToValue(pv)) pv = VpCopy(NULL, pv); pv->obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, pv); return pv->obj; diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c index 2bb6ce2..59d7e3b 100644 --- a/ext/pathname/pathname.c +++ b/ext/pathname/pathname.c @@ -1110,9 +1110,11 @@ path_unlink(VALUE self) * Creates a new Pathname object. */ static VALUE -path_f_pathname(VALUE self, VALUE str) +path_f_pathname(VALUE self, VALUE arg) { - return rb_class_new_instance(1, &str, rb_cPathname); + if (CLASS_OF(arg) == rb_cPathname) + return arg; + return rb_class_new_instance(1, &arg, rb_cPathname); } /* diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index a06adb1..0d1965d 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -51,6 +51,8 @@ class TestBigDecimal < Test::Unit::TestCase def test_global_new assert_equal(1, BigDecimal("1")) assert_equal(1, BigDecimal("1", 1)) + bd = BigDecimal.new("1", 1) + assert(bd.equal?(BigDecimal(bd))) assert_raise(ArgumentError) { BigDecimal("1", -1) } assert_raise(ArgumentError) { BigDecimal(4.2) } begin diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index ccdc5bd..b218ce0 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -437,6 +437,12 @@ class TestPathname < Test::Unit::TestCase assert_raise(ArgumentError) { Pathname.new("a\0") } end + def test_global_constructor + p = Pathname.new('a') + assert_equal(p, Pathname('a')) + assert(p.equal?(Pathname(p))) + end + class AnotherStringLike # :nodoc: def initialize(s) @s = s end def to_str() @s end diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index 0a7828b..5c66476 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -140,6 +140,8 @@ class Complex_Test < Test::Unit::TestCase assert_raise(TypeError){Complex(Object.new)} assert_raise(ArgumentError){Complex()} assert_raise(ArgumentError){Complex(1,2,3)} + c = Complex(1,0) + assert(c.equal?(Complex(c))) if (0.0/0).nan? assert_nothing_raised{Complex(0.0/0)}