What do file descriptors 3 and 4 mean in Ruby?

Run the code below in irb(without previous commands) get 5.

f = File.open("./test.txt")
puts f.fileno

File descriptor numbers 0, 1, 2standing STDIN, STDOUT, STDERR. What are 3 and 4 in a ruby?

Enviroment: Lubuntu 14.04 64bit, Ruby 1.9.3 under rvm.

+4
source share
2 answers

From standard input, output and error :

, 0, 1 2, , . , , 0 1 2, - . , 3, 4 ..

$stdin.fileno    # => 0
$stdout.fileno   # => 1
$stderr.fileno # => 2
File.open('test1').fileno # => 7
File.open('test2').fileno # => 8
File.open('test.txt').fileno # => 9

, for_fd.

File.for_fd(7) # => #<File:fd 7> # refers file test1
File.for_fd(8) # => #<File:fd 8> # refers file test2
File.for_fd(9) # => #<File:fd 9> # refers file test.txt

Opps!, , 3,4,5,6 RubyVM.

File.for_fd(3) # => 
# The given fd is not accessible because RubyVM reserves it (ArgumentError)
File.for_fd(4) # => 
# The given fd is not accessible because RubyVM reserves it (ArgumentError)
File.for_fd(5) # => 
# The given fd is not accessible because RubyVM reserves it (ArgumentError)
File.for_fd(6) # => 
# The given fd is not accessible because RubyVM reserves it (ArgumentError)

. Ruby - 2.0.0-p451 openSUSE13.1.

+3

. , . 0 1 2 , . , . 6 , , , , .

, , , , , . , .

+1

Source: https://habr.com/ru/post/1544579/


All Articles