It should be mentioned that both File.new and File.open are
able to receive file descriptors:
$ ruby -e ‘File.new(1).write 17’ | ruby -e ‘p File.new(0).read’
“17”
$ ruby -e ‘File.open(1){|f| f.write 17}’ | ruby -e ‘p File.open(0){|
f| f.read}’
“17”
$ ruby -e ‘f=File.new(“test”, “w”) ; File.open(f.fileno){|f| f.write
17}’
$ ruby -e ‘f=File.new(“test”) ; p File.open(f.fileno){|f| f.read}’
“17”
(note: 0==$stdin, 1==$stdout and 2==@stderr)
If you insist on using “-”, you might want to have a look on
either of the implementations below.
gegroet,
Erik V. - http://www.erikveen.dds.nl/
class YourFile < File
def self.new(file, mode=“r”, *perm)
if file == “-”
mode.include?(“r”) ? $stdin : $stdout
else
super(file, mode, *perm)
end
end
def self.open(file, mode=“r”, &block)
if file == “-”
super(new(file, mode).fileno, mode, &block)
else
super(file, mode, &block)
end
end
end
class File
@@_non_dashed_new = method(:new)
@@_non_dashed_open = method(:open)
def self.new(file, mode=“r”, *perm)
if file == “-”
mode.include?(“r”) ? $stdin : $stdout
else
@@_non_dashed_new.call(file, mode, *perm)
end
end
def self.open(file, mode=“r”, &block)
if file == “-”
@@_non_dashed_open.call(new(file, mode).fileno, mode, &block)
else
@@_non_dashed_open.call(file, mode, &block)
end
end
end