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

About this user

Luke Ivers http://technodolt.blogspot.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Ruby: make ranges always include the last value

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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS