I think you have a modelling problem, rather than an implementation problem.
Roman numerals are not numbers, they are a way of *representing* numbers
symbolically. (To be precise, they are a way of displaying natural
numbers). So you might want to write a NaturalNumber class and then provide
functions to write convert numbers to strings in Roman Numeral notation.
However, the natural numbers are just a subset of integers, so you might as
well stick with integers, and define preconditions on the roman numeral
printing functions that are tested with unit tests.
Cheers,
Nat.
----- Original Message -----
From: "Sean Russell" <ser / efn.org>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk / ruby-lang.org>; <undisclosed-recipients: ;>
Sent: Wednesday, November 07, 2001 1:21 PM
Subject: [ruby-talk:24507] Range weirdness
> Hi all,
>
> ruby 1.6.2 (2001-01-23) [i586-linux-gnu]
>
> I've got a class that I'd like to be a number, so I extended Numeric. I
> discovered, however, that when you try to turn these sorts of classes into
> Ranges, you get some odd, yet predictable, behavior.
>
> Here's an illustration:
>
> class RomanNumerals < Numeric
> ...
> end
>
> x = (RomanNumerals.new(1)..RomanNumerals.new(10))
> # -> (I..X)
>
> so get a proper range. However, for some reason I've yet to fathom,
> Range.each calls to_i on any Numerics it finds in the range, and goes from
> there. Therefore, if you've defined to_i for RomanNumerals, you get:
>
> x.to_a
> # -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>
> Behold the Holy Stack Trace:
>
> c-call -e:1 to_a Enumerable
> c-call -e:1 each Range
> call ./ternary.rb:56 to_i BTernary
> line ./ternary.rb:57 to_i BTernary
> return -e:1 to_i BTernary
> call ./ternary.rb:56 to_i BTernary
> line ./ternary.rb:57 to_i BTernary
> return -e:1 to_i BTernary
>
> Upon encountering "I", Range calls to_i, which returns 1, which it calls
> succ on, which of course returns 2, and so on.
>
> Although I'm sure Matz has a good reason for calling to_i on all Numerals,
> this behavior is a bit unexpected. I'd like to be able to drop in number
> replacements, but perhaps I'm subclassing the wrong class?
>
> Thanks,
>
> --- SER
>
>
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> Check out our new Unlimited Server. No Download or Time Limits!
> -----== Over 80,000 Newsgroups - 19 Different Servers! ==-----
>