Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelfeathers/56f34287dbb55dc0d4a2 to your computer and use it in GitHub Desktop.
Save michaelfeathers/56f34287dbb55dc0d4a2 to your computer and use it in GitHub Desktop.
An example of Literate Chaining Style in Ruby. The Kernel#c method is a pass through that serves as a placeholder for a comment and a convenient place for tracing.
# Accepts program file names on the command line and renders
# their text to stdout as a sequence of spoken words
token_map = {
"[" => "left square-bracket",
"]" => "right square-bracket",
"!" => "exclamation-point",
"\\" => "back slash",
"#" => "pound sign",
"\"" => "double-quote",
"$" => "dollar sign",
"%" => "percent sign",
"&" => "ampersand",
"'" => "single-quote",
"(" => "left-paren",
")" => "right-paren",
"*" => "star",
"+" => "plus sign",
"," => "comma",
"." => "dot",
"/" => "forward-slash",
":" => "colon",
";" => "semicolon",
"<" => "less-than sign",
"=" => "equals sign",
">" => "greater-than sign",
"?" => "question mark",
"@" => "at sign",
"^" => "caret",
"_" => "underscore",
"`" => "back tic",
"{" => "left curly-brace",
"|" => "vertical bar",
"}" => "right curly-brace",
"~" => "tilde",
"-" => "dash",
" " => "space",
"\n" => "new-line",
"\t" => "tab",
}
module Kernel; def c text; self; end; end
puts ARGF.c("all files as text") .read
.c("idents, nums, and single chars") .scan(/\w+|\W/)
.c("adding line numbers") .zip((0...Float::INFINITY))
.c("formatting") .map {|token,line_no| "#{line_no}: #{token_map[token] || token}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment