Skip to content

Instantly share code, notes, and snippets.

@mudge
Created October 8, 2012 13:11
Show Gist options
  • Save mudge/3852446 to your computer and use it in GitHub Desktop.
Save mudge/3852446 to your computer and use it in GitHub Desktop.
A tail-recursive Ruby method to split a date range into smaller month-based date ranges.
RubyVM::InstructionSequence.compile_option = {
:tailcall_optimization => true,
:trace_instruction => false
}
RubyVM::InstructionSequence.new(<<-RUBY).eval
def acc(start_date, end_date, accu)
if start_date >= end_date
accu + [end_date]
else
acc(start_date >> 1, end_date, accu + [start_date])
end
end
def months(start_date, end_date)
acc(start_date, end_date, [])
end
RUBY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment