ruby
42 lines · 8 steps
Counting business days in Ruby
A small calculator that skips weekends and holidays to count and add working days between dates.
Explained by
highlit
1require 'date'
2require 'set'
3
4class BusinessDayCalculator
5 DEFAULT_HOLIDAYS = Set[
6 Date.new(2024, 1, 1),
7 Date.new(2024, 7, 4),
8 Date.new(2024, 12, 25)
9 ].freeze
10
11 def initialize(holidays: DEFAULT_HOLIDAYS)
12 @holidays = holidays.to_set
13 end
14
15 def between(start_date, end_date)
16 start_date, end_date = end_date, start_date if start_date > end_date
17
18 (start_date...end_date).count { |date| business_day?(date) }
19 end
20
21 def add(start_date, days)
22 date = start_date
23 remaining = days
24
25 while remaining > 0
26 date += 1
27 remaining -= 1 if business_day?(date)
28 end
29
30 date
31 end
32
33 def business_day?(date)
34 !weekend?(date) && !@holidays.include?(date)
35 end
36
37 private
38
39 def weekend?(date)
40 date.saturday? || date.sunday?
41 end
42end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A frozen Set of holidays gives fast membership checks and a safe, shared default.
- 2Swapping arguments up front lets one method handle either date order cleanly.
- 3Building higher-level operations on one small predicate keeps the logic consistent and testable.
Related explainers
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
ruby
class SnippetHighlighter CONTEXT_RADIUS = 60 MAX_TERMS = 8
Building search-result snippets in Ruby
regular-expressions
text-processing
search
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/counting-business-days-in-ruby-explained-ruby-69fd/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.