Forum: Ruby Re: Range#size

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2006-04-13 15: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 Gray II

Well, that's slow but could...

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

...be a solution?

cheers

Simon
This topic is locked and can not be replied to.