ruby
38 lines · 5 steps
Parsing fixed-width records in Ruby
A data-driven parser turns fixed-width text lines into typed hashes using a declarative field table.
Explained by
highlit
1module FixedWidthParser
2 FIELDS = [
3 { name: :record_type, range: 0...2 },
4 { name: :account_id, range: 2...12, type: :integer },
5 { name: :last_name, range: 12...42, type: :string },
6 { name: :first_name, range: 42...62, type: :string },
7 { name: :balance, range: 62...74, type: :cents },
8 { name: :opened_on, range: 74...82, type: :date },
9 { name: :active, range: 82...83, type: :flag }
10 ].freeze
11
12 module_function
13
14 def parse(io)
15 io.each_line.filter_map do |line|
16 next if line.strip.empty?
17 parse_line(line.chomp)
18 end
19 end
20
21 def parse_line(line)
22 FIELDS.each_with_object({}) do |field, record|
23 raw = line[field[:range]].to_s
24 record[field[:name]] = cast(raw, field[:type])
25 end
26 end
27
28 def cast(raw, type)
29 case type
30 when :integer then Integer(raw.strip, 10)
31 when :string then raw.strip
32 when :cents then Integer(raw.strip, 10) / 100.0
33 when :date then Date.strptime(raw.strip, "%Y%m%d")
34 when :flag then raw.strip == "Y"
35 else raw
36 end
37 end
38end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Describing structure as data lets one small engine handle every field without branching per column.
- 2Ruby ranges over strings slice fixed-width columns cleanly, and filter_map drops blanks in one pass.
- 3Centralizing type coercion in a case statement keeps parsing logic in one place and easy to extend.
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
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
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
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 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/parsing-fixed-width-records-in-ruby-explained-ruby-dc3c/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.