class IO::FileDescriptor
inherits IO
¶
An IO
over a file descriptor.
Included modules
Crystal::System::FileDescriptor
IO::Buffered
Direct known subclasses
File
Class methods¶
Methods¶
#closed? : Bool
¶
View source
: Bool
#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.
#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.
#fd
¶
The raw file-descriptor. It is defined to be an Int
, but its size is
platform-specific.
#flock_exclusive(blocking = true)
¶
(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.
#flock_shared(blocking = true)
¶
(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.
#fsync(flush_metadata = true) : Nil
¶
(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.
#inspect(io : IO) : Nil
¶
(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>
#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
#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.
#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
#pos=(value)
¶
(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"
#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.
#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.
#seek(offset, whence : Seek = Seek::Set)
¶
(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"
#seek(offset, whence : Seek = Seek::Set
¶
(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.
#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