Skip to content

class IO::FileDescriptor
inherits IO

An IO over a file descriptor.

Included modules

Crystal::System::FileDescriptor IO::Buffered

Direct known subclasses

File

Class methods

.fcntl(fd, cmd, arg = 0)

View source

.new(fd, blocking = nil)

View source

Methods

#blocking

View source

#blocking=(value)

View source

#close_on_exec=(value : Bool)

View source

#close_on_exec?

View source

#closed? : Bool

Returns true if this IO is closed.

IO defines returns false, but including types may override.

View source

#cooked

Enables character processing for the duration of the given block. The so called cooked mode is the standard behavior of a terminal, doing line wise editing by the terminal and only sending the input to the program on a newline. Only call this when this IO is a TTY, such as a not redirected stdin.

View source

#cooked!

Enables character processing for this IO. The so called cooked mode is the standard behavior of a terminal, doing line wise editing by the terminal and only sending the input to the program on a newline. Only call this when this IO is a TTY, such as a not redirected stdin.

View source

#fcntl(cmd, arg = 0)

View source

#fd

The raw file-descriptor. It is defined to be an Int, but its size is platform-specific.

View source

#finalize

View source

#flock_exclusive(blocking = true

View source

#flock_exclusive(blocking = true)

Places an exclusive advisory lock. Only one process may hold an exclusive lock for a given file descriptor at a given time. IO::Error is raised if blocking is set to false and any existing lock is set.

View source

#flock_shared(blocking = true

View source

#flock_shared(blocking = true)

Places a shared advisory lock. More than one process may hold a shared lock for a given file descriptor at a given time. IO::Error is raised if blocking is set to false and an existing exclusive lock is set.

View source

#flock_unlock

Removes an existing advisory lock held by this process.

View source

#fsync(flush_metadata = true) : Nil

Flushes all data written to this File Descriptor to the disk device so that all changed information can be retrieved even if the system crashes or is rebooted. The call blocks until the device reports that the transfer has completed. To reduce disk activity the flush_metadata parameter can be set to false, then the syscall fdatasync will be used and only data required for subsequent data retrieval is flushed. Metadata such as modified time and access time is not written.

Note

Metadata is flushed even when flush_metadata is false on Windows and DragonFly BSD.

View source

#info

View source

#inspect(io : IO) : Nil

Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
View source

#noecho

Turns off character echoing for the duration of the given block. This will prevent displaying back to the user what they enter on the terminal. Only call this when this IO is a TTY, such as a not redirected stdin.

print "Enter password: "
password = STDIN.noecho &.gets.try &.chomp
puts
View source

#noecho!

Turns off character echoing for this IO. This will prevent displaying back to the user what they enter on the terminal. Only call this when this IO is a TTY, such as a not redirected stdin.

View source

#pos

Returns the current position (in bytes) in this IO.

File.write("testfile", "hello")

file = File.new("testfile")
file.pos     # => 0
file.gets(2) # => "he"
file.pos     # => 2
View source

#pos=(value)

Sets the current position (in bytes) in this IO.

File.write("testfile", "hello")

file = File.new("testfile")
file.pos = 3
file.gets_to_end # => "lo"
View source

#pretty_print(pp)

View source

#raw

Enables raw mode for the duration of the given block. In raw mode every keypress is directly sent to the program, no interpretation is done by the terminal. Only call this when this IO is a TTY, such as a not redirected stdin.

View source

#raw!

Enables raw mode for this IO. In raw mode every keypress is directly sent to the program, no interpretation is done by the terminal. Only call this when this IO is a TTY, such as a not redirected stdin.

View source

#reopen(other : IO::FileDescriptor)

View source

#seek(offset, whence : Seek = Seek::Set)

Seeks to a given offset (in bytes) according to the whence argument. Returns self.

File.write("testfile", "abc")

file = File.new("testfile")
file.gets(3) # => "abc"
file.seek(1, IO::Seek::Set)
file.gets(2) # => "bc"
file.seek(-1, IO::Seek::Current)
file.gets(1) # => "c"
View source

#seek(offset, whence : Seek = Seek::Set

Same as seek but yields to the block after seeking and eventually seeks back to the original position when the block returns.

View source

#tty?

Returns true if this IO is associated with a terminal device (tty), false otherwise.

IO returns false, but including types may override.

STDIN.tty?          # => true
IO::Memory.new.tty? # => false
View source

Macros

cooked_from_tc_mode!

View source

noecho_from_tc_mode!

View source

raw_from_tc_mode!

View source