Class: Redirect

Inherits:
IRB::StdioInputMethod
  • Object
show all
Defined in:
lib/fox16/irb.rb

Instance Method Summary collapse

Constructor Details

#initialize(dest) ⇒ Redirect

Returns a new instance of Redirect



41
42
43
44
# File 'lib/fox16/irb.rb', line 41

def initialize(dest)
  super()
  @dest = dest
end

Instance Method Details

#closeObject



56
57
58
59
60
61
62
63
64
# File 'lib/fox16/irb.rb', line 56

def close
  if @thread
    $defout = @old_out.dup
    $stderr = @old_err.dup
    $stdin = @old_in.dup
    @output[1].close
    @thread.join
  end
end

#getsObject



46
47
48
49
50
51
52
53
54
# File 'lib/fox16/irb.rb', line 46

def gets
  close
  @dest.write(prompt)
  str = @dest.gets(@prompt)
  if /^exit/ =~ str
    exit
  end
  @line[@line_no += 1] = str
end

#redirObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fox16/irb.rb', line 66

def redir
  @output = IO.pipe
  @old_out = $defout.dup
  @old_err = $stderr.dup
  @old_in = $stdin.dup
  $stdin = @dest.input[0]
  $defout = $stderr = @output[1]
  @output[1].fcntl(Fcntl::F_SETFL, File::NONBLOCK)
  @thread = Thread.new {
    while not @output[0].eof?
      select([@output[0]])
      @dest.write(@output[0].read)
    end
    @output[0].close
  }
end