From: Robert Klemme Date: 2011-04-07T20:53:34+09:00 Subject: Re: Hash Surprises with Fixnum, #hash, and #eql? On Thu, Apr 7, 2011 at 1:20 PM, Phillip Gawlowski wrote: > On Thu, Apr 7, 2011 at 11:19 AM, Robert Klemme > wrote: >> >> I also doubt whether it is a good idea to allow for subclassing of an >> integer like class.  What use case do you have in mind which would >> make this necessary? > > Complex numbers come to mind: Why bother, it has been done already. irb(main):006:0> x = Complex(0,-1) => (0-1i) irb(main):007:0> x * x => (-1+0i) irb(main):008:0> (x * x)+0 => (-1+0i) irb(main):009:0> (x * x).to_int => -1 And they do play nicely as ints - as long as it's possible: irb(main):011:0> %w{foo bar baz}[x*x] => "baz" irb(main):012:0> %w{foo bar baz}[x] RangeError: can't convert 0-1i into Integer from (irb):12:in `to_i' from (irb):12:in `to_int' from (irb):12:in `[]' from (irb):12 from /opt/bin/irb19:12:in `
' > 3 - 2j [+|*] 45^(j * e * 44°). > > Very different semantics for addition and multiplication of those than > for your normal space numbers, including conversion from Cartesian to > polar form. Bit of a textbook case for the benefits of inheritance and > function overloading. :P > > It'd be better to have those be a sub-class of Float, though. Actually it's Numeric which is correct because not every Complex _is a_ Float! irb(main):010:0> Complex.ancestors => [Complex, Numeric, Comparable, Object, Kernel, BasicObject] Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/