Project

General

Profile

Actions

Bug #7522

closed

Non-core "Type()" Kernel methods return new objects

Added by jballanc (Joshua Ballanco) over 11 years ago. Updated over 4 years ago.

Status:
Closed
Target version:
-
ruby -v:
2.0.0-preview1
[ruby-core:50596]

Description

The methods Array(), String(), Float(), Integer(), Hash(), and Rational() all return their argument when the argument is already an instance of the type in question. For example:

a = []
a.equal? Array(a) #=> true

However, the similar methods Pathname(), BigDecimal(), and Complex() do not do this:

p = Pathname.new('/tmp')
p.equal? Pathname(p) #=> false

I had the impression that the "Type()" methods were intended as "safe" coercion methods. That is, if no type conversion is required, then the system is left unchanged (and no new objects are created). The attached patch fixes the three methods mentioned above to adhere to this same invariant.


Files

kernel_methods.diff (2.97 KB) kernel_methods.diff jballanc (Joshua Ballanco), 12/06/2012 06:17 AM
kernel_methods.diff (3.05 KB) kernel_methods.diff jballanc (Joshua Ballanco), 12/10/2012 04:24 AM
kernel-pathname-bigdecimal-complex-return-arg-7522.patch (3.78 KB) kernel-pathname-bigdecimal-complex-return-arg-7522.patch jeremyevans0 (Jeremy Evans), 08/07/2019 04:11 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0