Range#size

Well, that's slow but could...

class Range
  def size
    inject(0){|s, dummy| s + 1}
  end
end

...be a solution?

cheers

Simon

···

-----Original Message-----
From: James Edward Gray II [mailto:james@grayproductions.net]
Sent: Thursday, April 13, 2006 3:26 PM
To: ruby-talk ML
Subject: Re: Range#size

On Apr 13, 2006, at 8:20 AM, Meinrad Recheis wrote:

> dear list,
>
> does anyone object to adding the method :size to class Range?
>
> class Range
> def size
> max-min+1
> end
> end

The problem with it is that it adds method dependancies to Range:

>> class Range
>> def size; max - min + 1 end
>> end
=> nil
>> ("AA".."BB").size
NoMethodError: undefined method `-' for "BB":String
         from (irb):2:in `size'
         from (irb):4
         from :0

James Edward Gray II