4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

標準入力の複製

4
Last updated at Posted at 2016-01-11

標準入力を2つに分ける。

def splitter(source, number = 2)
	pipes = number.times.map{ IO.pipe }
	Thread.fork do
		source.each_line { |line|
			pipes.each{|r, w| w.puts line }
		}
		pipes.each{|r, w| w.close}
	end
	return pipes.map{|r, w| r }
end

src_0, src_1 = splitter($stdin, 2)
src0.each_line { |line| puts line }
src1.each_line { |line| puts line }
4
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?