Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Last active December 1, 2024 19:54
Show Gist options
  • Save jgaskins/61908a4ec3e393117be3075633465e80 to your computer and use it in GitHub Desktop.
Save jgaskins/61908a4ec3e393117be3075633465e80 to your computer and use it in GitHub Desktop.
Driving IRB with Crystal
#!/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
➜ 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