Question: Need to example how to calculate the count of days in a period splited by month.
For example:

```
Wed, 25 Nov 2020 : Tue, 15 Dec 2020 => [6 (nov), 15(dec)]

```

Thank you!

Answer: 
This would be a job for `tally_by`, but that is not added to Ruby (yet?).
[tally](https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-tally) works too:

```
require 'date'

range = Date.parse("Wed, 25 Nov 2020") .. Date.parse("Tue, 15 Dec 2020")
p month_counts = range.map{|d| Date::ABBR_MONTHNAMES[d.month] }.tally
# => {"Nov"=>6, "Dec"=>15}

```
I would start by breaking the whole period into periods for each month.
Since Ruby has ranges, I'd write a helper method that takes a date range and yields month-ranges:

```
def each_month(range)
  return enum_for(__method__, range) unless block_given?

  date = range.begin.beginning_of_month
  loop do
    from = date.clamp(range)
    to   = (date.end_of_month).clamp(range)

    yield from..to

    date = date.next_month
    break unless range.cover?(date)
  end
end

```

[`clamp`](https://ruby-doc.org/core-2.7.1/Comparable.html#clamp-method) ensures that the range's bounds are taken into account when calculating each month's range. For Ruby version prior to 2.7 you have to pass the bounds separately:

```
from = date.clamp(range.begin, range.end)
to   = (date.end_of_month).clamp(range.begin, range.end)

```

Example usage:

```
from = '25 Nov 2020'.to_date
to   = '13 Jan 2021'.to_date

each_month(from..to).to_a
#=> [
#     Wed, 25 Nov 2020..Mon, 30 Nov 2020
#     Tue, 01 Dec 2020..Thu, 31 Dec 2020
#     Fri, 01 Jan 2021..Wed, 13 Jan 2021
#   ]

```

Now all we need is a way to count the days in each month-range: (e.g. via [`jd`](https://ruby-doc.org/stdlib-2.7.2/libdoc/date/rdoc/Date.html#method-i-jd))

```
def days(range)
  range.end.jd - range.begin.jd + 1
end

```

and some formatting:

```
each_month(from..to).map { |r| format('%d (%s)', days(r), r.begin.strftime('%b')) }
#=> ["6 (Nov)", "31 (Dec)", "13 (Jan)"]

```