From: Sean Russell Date: 2001-11-07T22:21:24+09:00 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! ==-----