Created
August 1, 2016 02:23
-
-
Save aereal/f62ec13194e5e0342f50cafc66032ddc to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
class Machine | |
INDENT = ' ' | |
def initialize | |
@indent_level = 0 | |
end | |
def incr_indent(n = 1) | |
@indent_level += n | |
end | |
def decr_indent(n = 1) | |
@indent_level -= n | |
end | |
def format(char) | |
case char | |
when /[\(\{\[]/ | |
incr_indent | |
char + next_line | |
when /[\)\}\]]/ | |
decr_indent | |
next_line + char | |
when ',' | |
char + next_line | |
else | |
char | |
end | |
end | |
private def next_line | |
indent = @indent_level > 0 ? INDENT * @indent_level : '' | |
"\n" + indent | |
end | |
end | |
machine = Machine.new | |
puts ARGF.read.each_char.map {|c| | |
machine.format(c) | |
}.join('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment