-
-
Save emiltin/d382d1527d771afa8130b174e323539f to your computer and use it in GitHub Desktop.
Run IRB inside Async
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 '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