るりまサーチ (Ruby 2.4.0)

最速Rubyリファレンスマニュアル検索!
22件ヒット [1-22件を表示] (0.050秒)
トップページ > クエリ:String[x] > バージョン:2.4.0[x] > クエリ:h[x] > クラス:IO[x]

別のキーワード

  1. string []=
  2. string []
  3. string slice
  4. string slice!
  5. string gsub

ライブラリ

キーワード

検索結果

IO#getch -> String (18607.0)

raw モードで一文字読み込んだ結果を返します。

raw モードで一文字読み込んだ結果を返します。

IO#readchar -> String (18607.0)

IO ポートから 1 文字読み込んで返します。 EOF に到達した時には EOFError が発生します。

...
IO
ポートから 1 文字読み込んで返します。
EOF に到達した時には EOFError が発生します。

テキスト読み込みメソッドとして動作します。

IO
#getc との違いは EOF での振る舞いのみです。

@raise EOFError EOF に到達した時に発生し...
...ます。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

f = File.new("testfile")
p f.readchar #=> "い"
p f.readchar #=> "ろ"
p f.readchar #=> "は"
f.read
f.readchar...
...#=> EOFError

@see IO#getc...

IO.write(path, string, **opts) -> Integer (676.0)

path で指定されるファイルを開き、string を書き込み、 閉じます。

...キーワード引数はファイルを開くときに使われ、エンコーディングなどを指定することができます。
詳しくは IO.open を見てください。

@param path ファイル名文字列
@param string 書き込む文字列
@param offset 書き込み開始位置
@par...
...s line three\nAnd so on...\n"
IO
.write("testfile", text) # => 66
IO
.write("testfile", "0123456789", 20) #=> 10
IO
.read("testfile")
# => "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
IO
.write("testfile", "0123456789") #=> 10
IO
.read("testfile")...
...# => "0123456789"
//}

@see IO.binwrite...

IO.write(path, string, offset=nil, **opts) -> Integer (676.0)

path で指定されるファイルを開き、string を書き込み、 閉じます。

...キーワード引数はファイルを開くときに使われ、エンコーディングなどを指定することができます。
詳しくは IO.open を見てください。

@param path ファイル名文字列
@param string 書き込む文字列
@param offset 書き込み開始位置
@par...
...s line three\nAnd so on...\n"
IO
.write("testfile", text) # => 66
IO
.write("testfile", "0123456789", 20) #=> 10
IO
.read("testfile")
# => "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
IO
.write("testfile", "0123456789") #=> 10
IO
.read("testfile")...
...# => "0123456789"
//}

@see IO.binwrite...

IO.binwrite(path, string, offset=nil) -> Integer (673.0)

path で指定されるファイルを開き、string を書き込み、 閉じます。

...書き込み、
閉じます。

ファイルを開くときの mode が "rb:ASCII-8BIT" で、バイナリモードが有効
である点以外は IO.write と同じです。

Kernel.#open と同様 path の先頭が "|" ならば、"|" に続くコマンドを実行し、コマンドの出力を...
...89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52

# binwriteを使用した場合: どの環境でも正しく保存できる。
IO
.binwrite('white.binmode.png', png)
puts IO.binread('white.binmode.png', 16).unpack('C*').map {|c| '%02x' % c }.join(' ')
# => 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 4...
...
IO
.write('white.txtmode.png', png)
puts IO.binread('white.txtmode.png', 16).unpack('C*').map {|c| '%02x' % c }.join(' ')
# => 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 (Linux/Macの場合
# => 89 50 4e 47 0d 0d 0a 1a 0d 0a 00 00 00 0d 49 48 (Windowsの場合
//}

@see c:IO#io_binmode, IO.w...

絞り込み条件を変える

IO#gets(limit, chomp: false) -> String | nil (613.0)

一行読み込んで、読み込みに成功した時にはその文字列を返します。 EOF に到達した時には nil を返します。

...il を返します。

テキスト読み込みメソッドとして動作します。

読み込んだ文字列を変数 $_ にセットします。
IO
#readline との違いは EOF での振る舞いのみです。

limit で最大の読み込みバイト数を指定します。ただし
ファイ...
...の読み込みバイト数
@param chomp true を指定すると各行の末尾から "\n", "\r", または "\r\n" を取り除きます。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

f = File.new("oneline_file")
f.gets...
...#=> "This is line one\n"
$_ #=> "This is line one\n"
f.gets #=> nil
$_ #=> nil

@see $/, IO#readline...

IO#gets(rs = $/, chomp: false) -> String | nil (613.0)

一行読み込んで、読み込みに成功した時にはその文字列を返します。 EOF に到達した時には nil を返します。

...il を返します。

テキスト読み込みメソッドとして動作します。

読み込んだ文字列を変数 $_ にセットします。
IO
#readline との違いは EOF での振る舞いのみです。

limit で最大の読み込みバイト数を指定します。ただし
ファイ...
...の読み込みバイト数
@param chomp true を指定すると各行の末尾から "\n", "\r", または "\r\n" を取り除きます。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

f = File.new("oneline_file")
f.gets...
...#=> "This is line one\n"
$_ #=> "This is line one\n"
f.gets #=> nil
$_ #=> nil

@see $/, IO#readline...

IO#gets(rs, limit, chomp: false) -> String | nil (613.0)

一行読み込んで、読み込みに成功した時にはその文字列を返します。 EOF に到達した時には nil を返します。

...il を返します。

テキスト読み込みメソッドとして動作します。

読み込んだ文字列を変数 $_ にセットします。
IO
#readline との違いは EOF での振る舞いのみです。

limit で最大の読み込みバイト数を指定します。ただし
ファイ...
...の読み込みバイト数
@param chomp true を指定すると各行の末尾から "\n", "\r", または "\r\n" を取り除きます。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

f = File.new("oneline_file")
f.gets...
...#=> "This is line one\n"
$_ #=> "This is line one\n"
f.gets #=> nil
$_ #=> nil

@see $/, IO#readline...

IO#readline(limit, chomp: false) -> String (613.0)

一行読み込んで、読み込みに成功した時にはその文字列を返します。 EOF に到達した時には EOFError が発生します。

...が発生します。

テキスト読み込みメソッドとして動作します。

読み込んだ文字列を変数 $_ にセットします。IO#gets との違いは EOF での振る舞いのみです。

limit で最大読み込みバイト数を指定します。ただしマルチバイト...
...と各行の末尾から "\n", "\r", または "\r\n" を取り除きます。

@raise EOFError EOF に到達した時に発生します。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

f = File.new("oneline_file")
f.readline...
...#=> "This is line one\n"
$_ #=> "This is line one\n"
f.readline #=> EOFError
$_ #=> nil

@see $/, IO#gets...

IO#readline(rs = $/, chomp: false) -> String (613.0)

一行読み込んで、読み込みに成功した時にはその文字列を返します。 EOF に到達した時には EOFError が発生します。

...が発生します。

テキスト読み込みメソッドとして動作します。

読み込んだ文字列を変数 $_ にセットします。IO#gets との違いは EOF での振る舞いのみです。

limit で最大読み込みバイト数を指定します。ただしマルチバイト...
...と各行の末尾から "\n", "\r", または "\r\n" を取り除きます。

@raise EOFError EOF に到達した時に発生します。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

f = File.new("oneline_file")
f.readline...
...#=> "This is line one\n"
$_ #=> "This is line one\n"
f.readline #=> EOFError
$_ #=> nil

@see $/, IO#gets...

絞り込み条件を変える

IO#readline(rs, limit, chomp: false) -> String (613.0)

一行読み込んで、読み込みに成功した時にはその文字列を返します。 EOF に到達した時には EOFError が発生します。

...が発生します。

テキスト読み込みメソッドとして動作します。

読み込んだ文字列を変数 $_ にセットします。IO#gets との違いは EOF での振る舞いのみです。

limit で最大読み込みバイト数を指定します。ただしマルチバイト...
...と各行の末尾から "\n", "\r", または "\r\n" を取り除きます。

@raise EOFError EOF に到達した時に発生します。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

f = File.new("oneline_file")
f.readline...
...#=> "This is line one\n"
$_ #=> "This is line one\n"
f.readline #=> EOFError
$_ #=> nil

@see $/, IO#gets...

IO#readlines(limit, chomp: false) -> [String] (613.0)

データを全て読み込んで、その各行を要素としてもつ配列を返します。 既に EOF に達していれば空配列 [] を返します。

...@param chomp true を指定すると各行の末尾から rs を取り除きます。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

//emlist[例][ruby]{
IO
.write("testfile", "line1,\nline2,\nline3,\n")
File.open("testfile") { |f| p f.readlines }...
...chomp = true)][ruby]{
IO
.write("testfile", "line1,\rline2,\r\nline3,\n")
File.open("testfile") { |f| p f.readlines(chomp: true) } # => ["line1,\rline2,", "line3,"]
File.open("testfile") { |f| p f.readlines("\r", chomp: true) } # => ["line1,", "line2,", "\nline3,\n"]
//}

@see $/, IO#gets...

IO#readlines(rs = $/, chomp: false) -> [String] (613.0)

データを全て読み込んで、その各行を要素としてもつ配列を返します。 既に EOF に達していれば空配列 [] を返します。

...@param chomp true を指定すると各行の末尾から rs を取り除きます。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

//emlist[例][ruby]{
IO
.write("testfile", "line1,\nline2,\nline3,\n")
File.open("testfile") { |f| p f.readlines }...
...chomp = true)][ruby]{
IO
.write("testfile", "line1,\rline2,\r\nline3,\n")
File.open("testfile") { |f| p f.readlines(chomp: true) } # => ["line1,\rline2,", "line3,"]
File.open("testfile") { |f| p f.readlines("\r", chomp: true) } # => ["line1,", "line2,", "\nline3,\n"]
//}

@see $/, IO#gets...

IO#readlines(rs, limit, chomp: false) -> [String] (613.0)

データを全て読み込んで、その各行を要素としてもつ配列を返します。 既に EOF に達していれば空配列 [] を返します。

...@param chomp true を指定すると各行の末尾から rs を取り除きます。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

//emlist[例][ruby]{
IO
.write("testfile", "line1,\nline2,\nline3,\n")
File.open("testfile") { |f| p f.readlines }...
...chomp = true)][ruby]{
IO
.write("testfile", "line1,\rline2,\r\nline3,\n")
File.open("testfile") { |f| p f.readlines(chomp: true) } # => ["line1,\rline2,", "line3,"]
File.open("testfile") { |f| p f.readlines("\r", chomp: true) } # => ["line1,", "line2,", "\nline3,\n"]
//}

@see $/, IO#gets...

IO.read(path, **opt) -> String | nil (613.0)

path で指定されたファイルを offset 位置から length バイト分読み込んで返します。

...場合は nil を返します。ただし、length に nil か 0 が指定されている場合は、空文字列 "" を返します。例えば、IO.read(空ファイル) は "" を返します。

引数 length が指定された場合はバイナリ読み込みメソッド、そうでない場合...
...e

IO
.open のモードを指定します。
"r" で始まる文字列である必要があります。

: :open_args

IO
.open に渡される引数を配列で指定します。

これらの他、 :external_encoding など
IO
.open のオプション引数が指定できます。

@see IO...
....binread

例:

IO
.read(empty_file) #=> ""
IO
.read(empty_file, 1) #=> nil
IO
.read(one_byte_file, 0, 10) #=> ""
IO
.read(one_byte_file, nil, 10) #=> ""
IO
.read(one_byte_file, 1, 10) #=> nil...

絞り込み条件を変える

IO.read(path, length = nil, **opt) -> String | nil (613.0)

path で指定されたファイルを offset 位置から length バイト分読み込んで返します。

...場合は nil を返します。ただし、length に nil か 0 が指定されている場合は、空文字列 "" を返します。例えば、IO.read(空ファイル) は "" を返します。

引数 length が指定された場合はバイナリ読み込みメソッド、そうでない場合...
...e

IO
.open のモードを指定します。
"r" で始まる文字列である必要があります。

: :open_args

IO
.open に渡される引数を配列で指定します。

これらの他、 :external_encoding など
IO
.open のオプション引数が指定できます。

@see IO...
....binread

例:

IO
.read(empty_file) #=> ""
IO
.read(empty_file, 1) #=> nil
IO
.read(one_byte_file, 0, 10) #=> ""
IO
.read(one_byte_file, nil, 10) #=> ""
IO
.read(one_byte_file, 1, 10) #=> nil...

IO.read(path, length = nil, offset = 0, **opt) -> String | nil (613.0)

path で指定されたファイルを offset 位置から length バイト分読み込んで返します。

...場合は nil を返します。ただし、length に nil か 0 が指定されている場合は、空文字列 "" を返します。例えば、IO.read(空ファイル) は "" を返します。

引数 length が指定された場合はバイナリ読み込みメソッド、そうでない場合...
...e

IO
.open のモードを指定します。
"r" で始まる文字列である必要があります。

: :open_args

IO
.open に渡される引数を配列で指定します。

これらの他、 :external_encoding など
IO
.open のオプション引数が指定できます。

@see IO...
....binread

例:

IO
.read(empty_file) #=> ""
IO
.read(empty_file, 1) #=> nil
IO
.read(one_byte_file, 0, 10) #=> ""
IO
.read(one_byte_file, nil, 10) #=> ""
IO
.read(one_byte_file, 1, 10) #=> nil...

IO.readlines(path, limit, chomp: false, opts={}) -> [String] (613.0)

path で指定されたファイルを全て読み込んで、その各行を要素としてもつ配列を返します。

...emlist[例][ruby]{
IO
.write("testfile", "line1\nline2,\nline3\n")
IO
.readlines("testfile") # => ["line1\n", "line2,\n", "line3\n"]
IO
.readlines("testfile", ",") # => ["line1\nline2,", "\nline3\n"]
//}

//emlist[例: rs を取り除く(chomp = true)][ruby]{
IO
.write("testfile",...
..."line1,\rline2,\r\nline3,\n")
IO
.readlines("testfile", chomp: true) # => ["line1,\rline2,", "line3,"]
IO
.readlines("testfile", "\r", chomp: true) # => ["line1,", "line2,", "\nline3,\n"]
//}...

IO.readlines(path, rs = $/, chomp: false, opts={}) -> [String] (613.0)

path で指定されたファイルを全て読み込んで、その各行を要素としてもつ配列を返します。

...emlist[例][ruby]{
IO
.write("testfile", "line1\nline2,\nline3\n")
IO
.readlines("testfile") # => ["line1\n", "line2,\n", "line3\n"]
IO
.readlines("testfile", ",") # => ["line1\nline2,", "\nline3\n"]
//}

//emlist[例: rs を取り除く(chomp = true)][ruby]{
IO
.write("testfile",...
..."line1,\rline2,\r\nline3,\n")
IO
.readlines("testfile", chomp: true) # => ["line1,\rline2,", "line3,"]
IO
.readlines("testfile", "\r", chomp: true) # => ["line1,", "line2,", "\nline3,\n"]
//}...

IO.readlines(path, rs, limit, chomp: false, opts={}) -> [String] (613.0)

path で指定されたファイルを全て読み込んで、その各行を要素としてもつ配列を返します。

...emlist[例][ruby]{
IO
.write("testfile", "line1\nline2,\nline3\n")
IO
.readlines("testfile") # => ["line1\n", "line2,\n", "line3\n"]
IO
.readlines("testfile", ",") # => ["line1\nline2,", "\nline3\n"]
//}

//emlist[例: rs を取り除く(chomp = true)][ruby]{
IO
.write("testfile",...
..."line1,\rline2,\r\nline3,\n")
IO
.readlines("testfile", chomp: true) # => ["line1,\rline2,", "line3,"]
IO
.readlines("testfile", "\r", chomp: true) # => ["line1,", "line2,", "\nline3,\n"]
//}...

絞り込み条件を変える

IO#read(length = nil, outbuf = "") -> String | nil (607.0)

length バイト読み込んで、その文字列を返します。

...の全てのデータを読み込んで、その文字列を返します。

@param outbuf 出力用のバッファを文字列で指定します。IO#read は読み込んだ
データをその文字列オブジェクトに上書きして返します。指定し
た文字...
...旦 length 長に拡張(ある
いは縮小)されたあと、実際に読み込んだデータのサイズになります。

@raise IOError 自身が読み込み用にオープンされていなければ発生します。

@raise Errno::EXXX データの読み込みに失敗した場...
...負の場合に発生します。

第二引数を指定した read の呼び出しでデータが空であった場合
(read が nil を返す場合)、outbuf は空文字列になります。

outbuf = "x" * 20;
io
= File.open("/dev/null")
p io.read(10,outbuf)
p outbuf
=> nil
""...

IO.binread(path, length = nil, offset = 0) -> String | nil (607.0)

path で指定したファイルを open し、offset の所まで seek し、 length バイト読み込みます。

...ist[例][ruby]{
IO
.write("testfile", "This is line one\nThis is line two\nThis is line three\nAnd so on...\n")
IO
.binread("testfile") # => "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
IO
.binread("testfile", 20) # => "This is line one\nThi"
IO
.binread("test...
...file", 20, 10) # => "ne one\nThis is line "
//}

@see IO.read...