Skip to content

Instantly share code, notes, and snippets.

@emiltin
Last active August 23, 2019 11:49
Show Gist options
  • Save emiltin/d382d1527d771afa8130b174e323539f to your computer and use it in GitHub Desktop.
Save emiltin/d382d1527d771afa8130b174e323539f to your computer and use it in GitHub Desktop.
Run IRB inside Async
#!/usr/bin/env ruby
require 'irb'
require 'async'
require 'async/io/protocol/line'
module IRB
# custom input class which reads from an async stream
class MyInputMethod < StdioInputMethod
def initialize
super
@input = Async::IO::Protocol::Line.new(
Async::IO::Stream.new(
Async::IO::Generic.new($stdin)
)
)
end
def gets
# since this is an async stream, the call to read_line()
# will give the Async reactor a chance to schedule other task
print @prompt
line = @input.read_line
@line[@line_no += 1] = "#{line}\n"
end
end
def self.start(ap_path = nil)
STDOUT.sync = true
$0 = File::basename(ap_path, ".rb") if ap_path
IRB.setup(ap_path)
if @CONF[:SCRIPT]
irb = Irb.new(nil, @CONF[:SCRIPT])
else
irb = Irb.new(nil,MyInputMethod.new)
end
irb.run(@CONF)
end
end
Async do
IRB.start(__FILE__)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment