Robert Klemme
5/22/2006 8:54:00 PM
Michael Perle <michael_perle@yahoo.com> wrote:
> Robert Klemme wrote:
>> Michael Perle wrote:
>>
>>> Hi Ruby Folks,
>>>
>>> What do you think would be the best way
>>> to provide one and the same method as a
>>> class method and as an instance method.
> [...]
>> There are several ways but before that I'd ask what you want to
>> accomplish here.
>
> The idea was to provide a method out of one class
> to those who need only these methods and not
> necessarily an instance.
> Numbering.roman(12) should deliver 'XII'.
You can as well create a class Roman with a proper constructor and a to_s
method. In that case you should also implement <=>, coerce, to_i, to_int,
hash and probably some more. You can than also implement Kernel.Roman()
like Float() etc.
irb(main):002:0> Float("1.2")
=> 1.2
>> A common way to do this is a module
>
> Thanks. Agree. I had locked in myself to
> this one and only class definition.
>
> Thinking a bit further, it could make sense
> add a method like 'to_roman' or so to the
> class Integer at runtime. Would that go too
> far or is that indeed the rubyish way?
No that's perfectly ok.
Kind regards
robert