Instantly share code, notes, and snippets.

What would you like to do?
# A simpler example: create an object which takes
# in a file name and allows you to iterate over
# only the comments (lines that start with '#')
# Obviously, it'd be pretty easy to do this inline, but it's just an
# example. One could also imagine it returning every set of
# contiguous comment lines as a single comment, or even being smart enough to
# do C-style /* ... */ comments and extract those.
# The point is that you've got that all hidden in a class, and the user
# of it just needs to call #each or #find or whatever
# Note that it's pretty clear how you'd turn this into the 'grep' command
class CommentReader
include Enumerable
# Should take either a filename or an IO object, but whatever
def initialize(filename)
@file =, 'r:utf-8')
def each(&blk)
@file.each_line do |line|
if line =~ /\A\s*#/ # does it start with optional space and a hash?
yield line.chomp
# don't do a damn thing. Just go to the top and get the next line.
require 'marc'
class AlephSquentialFile
include Enumerable;
def initialize(file_or_io_object)
@file = file_or_io_object
def turn_a_bunch_of_lines_into_a_MARC_record(bunch_of_lines)
# implementation left up to the reader
# Throw an AlephSquentialFile::Malformed if something is wrong
# return a MARC::Record object on success
# By defining #each, I get all the other Enumerable
# methods for free. Of course, with a 2GB file of these
# things, you probably don't want to call, e.g., #map
# unless you're sure you're doing it lazily. Then again,
# memory *is* cheap these days...
# In real life I'd capture the line number and resuce any errors
# so one bad line wouldn't grind the whole thing to a halt and
# could just be logged
def each(&blk)
@file.each_line.slice_when{|a,b| a[0..8] != b[0..8]}.each do |bunch_of_lines|
record = turn_a_bunch_of_lines_into_a_MARC_record(bunch_of_lines)
yield record # exactly the same as
# Now I can just use it as an iterator that spits out MARC::Record objects
# and not care what's going on underneath.
# Get the titles'myfile.seq')).each do |record|
title = record['245'] # record is a MARC::Record object
puts title
# Find the record with the latest publication date, stored in chars 7-10 of the 008 field
# Look! I get to use max_by!
latest_record ='myfile.seq')).max_by{|r| r['008'].value[7..10].to_i}
# Find everything written by me. Again, be careful about getting giant sets back in real life.
bills_stuff ='myfile.seq')).find_all{|r| =~ /dueberb/i}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment