Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Ruby: make ranges always include the last value (See related posts)

So, in conjunction with other snipped about setting up intervals on Time, this can be added to Range to make sure it always includes the last value, even if the interval doesn't directly land you on the end value

class Range
  def each(options = {}, &block)
    val = self.begin
    while val < self.end
      yield val
      val = val.succ
    end
    yield self.end if self.end == val || options[:always_include_last]
  end
end


You need to create an account or log in to post comments to this site.


Click here to browse all 4860 code snippets

Related Posts