Bug #7522

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

Added by Joshua Ballanco over 1 year ago. Updated about 1 year ago.

[ruby-core:50596]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:next minor
ruby -v:2.0.0-preview1 Backport:

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.

kernel_methods.diff Magnifier (2.97 KB) Joshua Ballanco, 12/06/2012 06:17 AM

kernel_methods.diff Magnifier (3.05 KB) Joshua Ballanco, 12/10/2012 04:24 AM

History

#1 Updated by Charlie Somerville over 1 year ago

=begin
Your change to ext/bigdecimal/bigdecimal.c will cause a compiler warning:

compiling bigdecimal.c
bigdecimal.c: In function ‘BigDecimalglobalnew’:
bigdecimal.c:2414: warning: ISO C90 forbids mixed declarations and code

You should move the declaration of (({pv})) above the your if statement, but leave the assignment where it is.
=end

#2 Updated by Joshua Ballanco over 1 year ago

Ah, thanks for that catch!

Updated patch is attached.

#3 Updated by Koichi Sasada about 1 year ago

  • Category set to core
  • Assignee set to Yusuke Endoh
  • Target version set to 2.0.0

I'm not sure who should take this issue.

#4 Updated by Yusuke Endoh about 1 year ago

  • Status changed from Open to Assigned
  • Assignee changed from Yusuke Endoh to Yukihiro Matsumoto
  • Target version changed from 2.0.0 to next minor

Sorry but let me postpone this to the next minor.
I'm afraid if the existing code depends on the traditional behavior.
It should have been included in rc1, which is my fault. Sorry, blame me.

Yusuke Endoh mame@tsg.ne.jp

#5 Updated by Joshua Ballanco about 1 year ago

No apology necessary. Thanks for the help!

Also available in: Atom PDF