Last active
August 29, 2015 14:02
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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