Code Explainers

Code explainers tagged #enumerable

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 ActivityFeed
  include Enumerable
 
  Entry = Struct.new(:record, :type, :occurred_at, keyword_init: true) do

Building a paginated activity feed in Rails

enumerable pagination composition
Intermediate 9 steps
ruby
class WeightedMovingAverageForecaster
  DEFAULT_WINDOW = 5
 
  def initialize(window: DEFAULT_WINDOW, weights: nil)

A weighted moving average forecaster in Ruby

forecasting weighted-average normalization
Intermediate 8 steps
ruby
class HashDiff
  Change = Struct.new(:from, :to)
 
  def self.call(before, after)

Diffing two hashes in Ruby

set-operations struct enumerable
Intermediate 7 steps
ruby
module FixedWidthParser
  FIELDS = [
    { name: :record_type, range: 0...2 },
    { name: :account_id,  range: 2...12, type: :integer },

Parsing fixed-width records in Ruby

parsing data-driven-design type-coercion
Intermediate 5 steps
ruby
class DateRangeMerger
  def initialize(ranges)
    @ranges = ranges
  end

Merging overlapping date ranges in Ruby

sorting interval-merging enumerable
Intermediate 5 steps
ruby
require "forwardable"
 
class ShoppingCart
  extend Forwardable

Delegation in Ruby with Forwardable

delegation composition enumerable
Intermediate 9 steps
ruby
class ContactImporter
  def initialize(rows)
    @rows = rows
  end

Deduplicating contacts with Enumerable#uniq

deduplication normalization enumerable
Intermediate 4 steps
ruby
class MetricSeries
  def initialize(readings, window: 5)
    @readings = readings
    @window = window

Rolling averages with each_cons in Ruby

sliding-window enumerable data-smoothing
Intermediate 7 steps
ruby
require "csv"
 
class SalesReport
  def initialize(path)

Aggregating CSV sales data in Ruby

data-aggregation memoization group_by
Intermediate 6 steps
ruby
module TextAnalysis
  module_function
 
  WORD_PATTERN = /[\p{Alpha}']+/

Building a word-frequency analyzer in Ruby

text processing regex enumerable
Intermediate 8 steps
ruby
class LinkedList
  include Enumerable
 
  Node = Struct.new(:value, :next_node)

Building an enumerable linked list in Ruby

linked-list enumerable data-structures
Intermediate 7 steps