On Thu, Apr 7, 2011 at 6:05 AM, Clifford Heath <no / spam.please.net> wrote:
> I have a class which delegates for Integer, and wants to behave as much
> like a real Integer as possible (except for being able to be subclassed).

There's still a lot missing for a number replacement.  Please see
http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

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?

> It *mostly* works... but falls foul of Ruby's various hacks, errors, and
> internal optimisations in the Fixnum and Hash classes.

> In particular, the Hash implementations work (and break!) differently in
> MRI, Rubinius and JRuby. It's documented to use only #hash and #eql?,
> but that's not always true (sometimes these have hard-wired optimsations).

When you violate contracts you cannot expect code to work properly.

> The Hash documentation does not say whether #eql? will be called only on
> items in the hash, or only on keys being used to probe the hash. It should
> be one or the other, since a.eql?(b) might not always mean b.eql?(a).

But that is the contract as far as I can see.  Having different
results for both violates the equivalence relation which means all
bets are off.

> Please peruse this code: <https://gist.github.com/906998>, try it on the
> various Ruby versions, and also try it with the Fixnum monkey-patches
> removed.
>
> You'll see that the behaviour is very unpredictable.

Yes, because of your violation of the contract.  You have there a nice
demonstration why it is a bad idea most of the time to fiddle with
core class method implementations.  They are used everywhere and you
cannot foresee the effects of changing their implementation on other
code.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/