Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some final cleaning of the hacking spree, before speccing
- Loading branch information
Showing
6 changed files
with
81 additions
and
101 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module IRB | ||
class << self | ||
attr_accessor :driver_class | ||
|
||
def driver=(driver) | ||
Thread.current[:irb_driver] = driver | ||
end | ||
|
||
def driver | ||
current_thread = Thread.current | ||
current_thread[:irb_driver] ||= begin | ||
if group = current_thread.group | ||
group.list.each do |thread| | ||
break(driver) if driver = thread[:irb_driver] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
module Driver | ||
class OutputRedirector | ||
def self.target | ||
if driver = IRB.driver | ||
driver.output | ||
else | ||
$stderr | ||
end | ||
end | ||
|
||
# A standard output object has only one mandatory method: write. | ||
# It returns the number of characters written | ||
def write(object) | ||
string = object.respond_to?(:to_str) ? object : object.to_s | ||
send_to_target :write, string | ||
string.length | ||
end | ||
|
||
# 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 | ||
def puts(*args) | ||
send_to_target :puts, *args | ||
nil | ||
end | ||
|
||
# Override this if for your situation you need to dispatch from a thread | ||
# in a special manner. | ||
# | ||
# TODO: for macruby send to main thread | ||
def send_to_target(method, *args) | ||
self.class.target.__send__(method, *args) | ||
end | ||
end | ||
end | ||
end |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require File.expand_path('../../spec_helper', __FILE__) | ||
|
||
describe "IRB::Driver::TTY" do | ||
|
||
end |