Forum: Ruby Re: Range#size

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Kroeger, Simon (ext) (Guest)
on 2006-04-13 17:46
(Received via mailing list)
> > does anyone object to adding the method :size to 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 G. II

Well, that's slow but could...

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


This topic is locked and can not be replied to.