This can also be done using IO.expect
require 'pty' require 'expect' str = "RUBY_VERSION" PTY.spawn("irb") do |reader, writer| reader.expect(/0> /) writer.puts(str) reader.expect(/=> /) answer = reader.gets puts "Ruby version from irb: #{answer}" end
It is expected that the spawned process will display "0>" (the end of the irb prompt) and when it sees that it is printing a specific line. He then searches for the IRB, waiting for it to display "=>" and capture the returned data.
source share