Class: IRB::Driver::OutputRedirector

Inherits:
Object
  • Object
show all
Defined in:
lib/irb/driver.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.targetObject



24
25
26
27
28
29
30
# File 'lib/irb/driver.rb', line 24

def self.target
  if driver = IRB::Driver.current
    driver.output
  else
    $stderr
  end
end

Instance Method Details

#puts(*args) ⇒ Object

if puts is not there, Ruby will automatically use the write method when calling Kernel#puts, but defining it has 2 advantages:

  • if puts is not defined, you cannot of course use $stdout.puts directly

  • (objc) when Ruby emulates puts, it calls write twice (once for the string and once for the carriage return) but here we send the calls to another thread so it’s nice to be able to save up one (slow) interthread call



47
48
49
50
# File 'lib/irb/driver.rb', line 47

def puts(*args)
  send_to_target :puts, *args
  nil
end

#send_to_target(method, *args) ⇒ Object

Override this if for your situation you need to dispatch from a thread in a special manner.

TODO: for macruby send to main thread



56
57
58
# File 'lib/irb/driver.rb', line 56

def send_to_target(method, *args)
  self.class.target.__send__(method, *args)
end

#write(object) ⇒ Object

A standard output object has only one mandatory method: write. It returns the number of characters written



34
35
36
37
38
# File 'lib/irb/driver.rb', line 34

def write(object)
  string = object.respond_to?(:to_str) ? object : object.to_s
  send_to_target :write, string
  string.length
end