Class: Expletive::Dump
- Inherits:
-
Object
- Object
- Expletive::Dump
- Defined in:
- lib/expletive/filters.rb
Instance Method Summary collapse
- #human_readable?(byte) ⇒ Boolean
-
#initialize(in_io = $stdin, out_io = $stdout) ⇒ Dump
constructor
A new instance of Dump.
- #run ⇒ Object
- #start_new_line ⇒ Object
- #write_string(s) ⇒ Object
Constructor Details
#initialize(in_io = $stdin, out_io = $stdout) ⇒ Dump
Returns a new instance of Dump.
8 9 10 11 12 |
# File 'lib/expletive/filters.rb', line 8 def initialize(in_io=$stdin, out_io=$stdout) @in = in_io @out = out_io @current_width = 0 end |
Instance Method Details
#human_readable?(byte) ⇒ Boolean
42 43 44 |
# File 'lib/expletive/filters.rb', line 42 def human_readable?(byte) (byte >= SPACE) && (byte <= TILDE) end |
#run ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/expletive/filters.rb', line 14 def run n = 0 until @in.eof? byte = @in.readbyte case when byte == BACKSLASH write_string "\\\\" when byte == NEWLINE write_string "\\n" when human_readable?(byte) write_string byte.chr else write_string "\\%02x" % byte end end end |
#start_new_line ⇒ Object
31 32 33 34 |
# File 'lib/expletive/filters.rb', line 31 def start_new_line @out.print("\n") @current_width = 0 end |
#write_string(s) ⇒ Object
36 37 38 39 40 |
# File 'lib/expletive/filters.rb', line 36 def write_string(s) @out.print(s) @current_width += s.size start_new_line if @current_width > 60 end |