Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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