Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Ruby Programming/Reference/Objects/IO

From Wikibooks, open books for an open world
<Ruby Programming |Reference |Objects
This page may need to bereviewed for quality.

The IO class is basically an abstract class that provides methods to use on streams (for example, open files, or open sockets).

Basically, you can call several different things on it.

Encoding

[edit |edit source]

Note that with 1.9, each call will return you a String with an encoding set, based on either how the connection was established, ex:

a = File.new('some filename', 'rb:ASCII-8BIT') # strings from this will be read in as ASCII-8BITb = File.new('some filename', 'r') # strings from this will be read in as whatever the Encoding.default_external is# you can change what the encoding will bea.set_encoding "UTF-8" # from now on it will read in UTF-8

gets

[edit |edit source]

gets reads exactly one line, or up to the end of the file/stream (and includes the trailing newline, if one was given). A blocking call.

recv

[edit |edit source]

recv(1024) reads up to at most 1024 bytes and returns your the String. A blocking call. Reads "" if a socket has been closed gracefully from the other end. It also has non blocking companions.

read

[edit |edit source]

read reads up to the end of the file or up to when the socket is closed. Returns any number of bytes. Blocking.For information on how to avoid blocking, seeSocket.

Retrieved from "https://en.wikibooks.org/w/index.php?title=Ruby_Programming/Reference/Objects/IO&oldid=3569226"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp