Last active
December 1, 2024 19:54
-
-
Save jgaskins/61908a4ec3e393117be3075633465e80 to your computer and use it in GitHub Desktop.
Driving IRB with Crystal
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 crystal | |
reader, writer = IO.pipe | |
# Write your script to drive IRB here | |
spawn do | |
sleep 1.second | |
writer.puts "1+1" | |
sleep 1.second | |
writer.puts "Object.new" | |
sleep 1.second | |
ensure | |
writer.close | |
end | |
File.open("output.txt", "w") do |output_txt| | |
output = IO::MultiWriter.new( | |
STDOUT, | |
output_txt, | |
) | |
irb = Process.new( | |
"irb", | |
args: %w[--prompt classic], | |
input: reader, # When we write to the writer, it will read from the reader | |
output: output, | |
error: output, | |
) | |
irb.wait | |
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
➜ driving_irb cat output.txt | |
Switch to inspect mode. | |
irb(main):001:0> 1+1 | |
2 | |
irb(main):002:0> Object.new | |
#<Object:0x000000011be87918> | |
irb(main):003:0> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment