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



31
32
33
34
35
36
37
# File 'lib/irb/driver.rb', line 31

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



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

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



63
64
65
# File 'lib/irb/driver.rb', line 63

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



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

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