Last active
December 14, 2015 04:29
-
-
Save takkkun/5028651 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
puts 1 | |
puts %w(hello world) * ' ' | |
puts %w(h l t).join('igh') |
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 | |
require 'stringio' | |
path = ARGV.first or abort('') | |
class OutputRecorder < StringIO | |
def self.record | |
original_stdout = $stdout | |
$stdout = output_recorder = new | |
yield | |
output_recorder.outputs.extend(OutputsHelper) | |
ensure | |
$stdout = original_stdout | |
end | |
def initialize | |
super | |
close_read | |
@outputs = {} | |
end | |
attr_reader :outputs | |
def write(string) | |
file, line_number = caller.first.split(':') | |
line_number = line_number.to_i | |
@outputs[file] ||= {} | |
@outputs[file][line_number] ||= '' | |
@outputs[file][line_number] << string | |
super | |
end | |
module OutputsHelper | |
def record?(file, line_number) | |
!!(self[file] && self[file][line_number]) | |
end | |
def [](*args) | |
return super unless args.size == 2 | |
file, line_number = args | |
self[file] && self[file][line_number] | |
end | |
end | |
end | |
outputs = OutputRecorder.record { load path } | |
File.open(path, 'r') do |file| | |
file.each_line.with_index do |line, index| | |
print line.chomp | |
print " # => #{outputs[path, index + 1].chomp}" if outputs.record?(path, index + 1) | |
puts | |
end | |
end |
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
$ ./exprint.rb example.rb | |
puts 1 # => 1 | |
puts %w(hello world) * ' ' # => hello world | |
puts %w(h l t).join('igh') # => highlight |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment