るりまサーチ

最速Rubyリファレンスマニュアル検索!
61件ヒット [1-61件を表示] (0.041秒)
トップページ > クエリ:path[x] > クエリ:write[x] > クエリ:foreach[x]

別のキーワード

  1. _builtin path
  2. pathname to_path
  3. _builtin absolute_path
  4. _builtin to_path
  5. pstore path

ライブラリ

クラス

キーワード

検索結果

IO.foreach(path, rs = $/, chomp: false) -> Enumerator (18314.0)

path で指定されたファイルの各行を引数としてブロックを繰り返し実行します。 path のオープンに成功すれば nil を返します。

...
path
で指定されたファイルの各行を引数としてブロックを繰り返し実行します。
path
のオープンに成功すれば nil を返します。

ブロックが与えられなかった場合は、path で指定されたファイルの各行を繰り返す
Enumerator オブ...
...除きます。

@raise Errno::EXXX path のオープンに失敗した場合、発生します。

//emlist[例:rs 指定なし][ruby]{
IO.write("testfile", "line1\nline2,\nline3\n")
IO.foreach("testfile") # => #<Enumerator: IO:foreach("testfile")>
IO.foreach("testfile") { |x| print "GOT ", x }
#...
...rs = "," )][ruby]{
IO.write("testfile", "line1,line2,line3")
IO.foreach("testfile", ",") { |x| puts "GOT #{x}" }
# => GOT line1,
# GOT line2,
# GOT line3
//}

//emlist[例: 各行の末尾から "\n", "\r", または "\r\n" を取り除く(chomp = true)][ruby]{
IO.write("testfile", "line1\nlin...

IO.foreach(path, rs = $/, chomp: false) {|line| ... } -> nil (18314.0)

path で指定されたファイルの各行を引数としてブロックを繰り返し実行します。 path のオープンに成功すれば nil を返します。

...
path
で指定されたファイルの各行を引数としてブロックを繰り返し実行します。
path
のオープンに成功すれば nil を返します。

ブロックが与えられなかった場合は、path で指定されたファイルの各行を繰り返す
Enumerator オブ...
...除きます。

@raise Errno::EXXX path のオープンに失敗した場合、発生します。

//emlist[例:rs 指定なし][ruby]{
IO.write("testfile", "line1\nline2,\nline3\n")
IO.foreach("testfile") # => #<Enumerator: IO:foreach("testfile")>
IO.foreach("testfile") { |x| print "GOT ", x }
#...
...rs = "," )][ruby]{
IO.write("testfile", "line1,line2,line3")
IO.foreach("testfile", ",") { |x| puts "GOT #{x}" }
# => GOT line1,
# GOT line2,
# GOT line3
//}

//emlist[例: 各行の末尾から "\n", "\r", または "\r\n" を取り除く(chomp = true)][ruby]{
IO.write("testfile", "line1\nlin...

IO.foreach(path, rs = $/) -> Enumerator (18302.0)

path で指定されたファイルの各行を引数としてブロックを繰り返し実行します。 path のオープンに成功すれば nil を返します。

...
path
で指定されたファイルの各行を引数としてブロックを繰り返し実行します。
path
のオープンに成功すれば nil を返します。

ブロックが与えられなかった場合は、path で指定されたファイルの各行を繰り返す
Enumerator オブ...
...フモード)。


@raise Errno::EXXX path のオープンに失敗した場合、発生します。

//emlist[例:rs 指定なし][ruby]{
IO.write("testfile", "line1\nline2,\nline3\n")
IO.foreach("testfile") # => #<Enumerator: IO:foreach("testfile")>
IO.foreach("testfile") { |x| print "GOT ", x }...
...# => GOT line1
# GOT line2,
# GOT line3
//}

//emlist[例:カンマを行の区切りに指定( rs = "," )][ruby]{
IO.write("testfile", "line1,line2,line3")
IO.foreach("testfile", ",") { |x| puts "GOT #{x}" }
# => GOT line1,
# GOT line2,
# GOT line3
//}


@see $/...

IO.foreach(path, rs = $/) {|line| ... } -> nil (18302.0)

path で指定されたファイルの各行を引数としてブロックを繰り返し実行します。 path のオープンに成功すれば nil を返します。

...
path
で指定されたファイルの各行を引数としてブロックを繰り返し実行します。
path
のオープンに成功すれば nil を返します。

ブロックが与えられなかった場合は、path で指定されたファイルの各行を繰り返す
Enumerator オブ...
...フモード)。


@raise Errno::EXXX path のオープンに失敗した場合、発生します。

//emlist[例:rs 指定なし][ruby]{
IO.write("testfile", "line1\nline2,\nline3\n")
IO.foreach("testfile") # => #<Enumerator: IO:foreach("testfile")>
IO.foreach("testfile") { |x| print "GOT ", x }...
...# => GOT line1
# GOT line2,
# GOT line3
//}

//emlist[例:カンマを行の区切りに指定( rs = "," )][ruby]{
IO.write("testfile", "line1,line2,line3")
IO.foreach("testfile", ",") { |x| puts "GOT #{x}" }
# => GOT line1,
# GOT line2,
# GOT line3
//}


@see $/...

Pathname#each_line(*args) -> Enumerator (3052.0)

IO.foreach(self.to_s, *args, &block) と同じです。

...IO.foreach(self.to_s, *args, &block) と同じです。

//emlist[例][ruby]{
require "pathname"

IO.write("testfile", "line1\nline2,\nline3\n")
Path
name("testfile").each_line
# => #<Enumerator: IO:foreach("testfile")>
//}

//emlist[例 ブロックを指定][ruby]{
require "pathname"

IO.write("tes...
...tfile", "line1\nline2,\nline3\n")
Path
name("testfile").each_line {|f| p f }

# => "line1\n"
# => "line2,\n"
# => "line3\n"
//}

//emlist[例 limit を指定][ruby]{
require "pathname"

IO.write("testfile", "line1\nline2,\nline3\n")
Path
name("testfile").each_line(4) {|f| p f }

# => "line"
# => "1\n"...
...# => "line"
# => "2,\n"
# => "line"
# => "3\n"
//}

//emlist[例 sep を指定][ruby]{
require "pathname"

IO.write("testfile", "line1\nline2,\nline3\n")
Path
name("testfile").each_line(",") {|f| p f }

# => "line1\nline2,"
# => "\nline3\n"
//}

@see IO.foreach...

絞り込み条件を変える

Pathname#each_line(*args) {|line| ... } -> nil (3052.0)

IO.foreach(self.to_s, *args, &block) と同じです。

...IO.foreach(self.to_s, *args, &block) と同じです。

//emlist[例][ruby]{
require "pathname"

IO.write("testfile", "line1\nline2,\nline3\n")
Path
name("testfile").each_line
# => #<Enumerator: IO:foreach("testfile")>
//}

//emlist[例 ブロックを指定][ruby]{
require "pathname"

IO.write("tes...
...tfile", "line1\nline2,\nline3\n")
Path
name("testfile").each_line {|f| p f }

# => "line1\n"
# => "line2,\n"
# => "line3\n"
//}

//emlist[例 limit を指定][ruby]{
require "pathname"

IO.write("testfile", "line1\nline2,\nline3\n")
Path
name("testfile").each_line(4) {|f| p f }

# => "line"
# => "1\n"...
...# => "line"
# => "2,\n"
# => "line"
# => "3\n"
//}

//emlist[例 sep を指定][ruby]{
require "pathname"

IO.write("testfile", "line1\nline2,\nline3\n")
Path
name("testfile").each_line(",") {|f| p f }

# => "line1\nline2,"
# => "\nline3\n"
//}

@see IO.foreach...

NEWS for Ruby 2.6.0 (48.0)

NEWS for Ruby 2.6.0 このドキュメントは前回リリース以降のバグ修正を除くユーザーに影響のある機能の変更のリストです。

...proc やメソッドに対応する AST ノードを返します。 [実験的]

* RubyVM
* 新規メソッド
* RubyVM.resolve_feature_path は "require(feature)" で読み込むファイルを
特定します。 [実験的] 15230

* String
* String#crypt は非推奨に...
...* Vector#[]=

* Net::HTTP
* 新規オプション
* :write_timeout キーワード引数が Net::HTTP.new に追加されました。 13396
* 新規メソッド
* Net::HTTP#write_timeout と Net::HTTP#write_timeout= が追加されました。 13396
* 新規定数...
...渡すのは非推奨になる予定で、
今は警告が出ます。 14643

* File
* File.read, File.binread, File.write, File.binwrite,
File.foreach, File.readlines はパスがパイプ文字 '|' で始まっていても
外部コマンドを実行しなくなりました...

CSV (24.0)

このクラスは CSV ファイルやデータに対する完全なインターフェイスを提供します。

...=== 読み込み

//emlist[][ruby]{
require "csv"

csv_text = <<~CSV_TEXT
Ruby,1995
Rust,2010
CSV_TEXT

IO.write "sample.csv", csv_text

# ファイルから一行ずつ
CSV.foreach("sample.csv") do |row|
p row
end
# => ["Ruby", "1995"]
# ["Rust", "2010"]

# ファイルから一度に
p...
...csv_text)
# => [["Ruby", "1995"], ["Rust", "2010"]]
//}

=== 書き込み

//emlist[][ruby]{
require 'csv'

# ファイルへ書き込み
CSV.open("path/to/file.csv", "wb") do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ...
end

# 文字列へ書き込み
csv_string =...
...d String objects
passed into CSV have the proper Encoding set and everything should just work.
CSV methods that allow you to open IO objects (CSV::foreach(), CSV::open(),
CSV::read(), and CSV::readlines()) do allow you to specify the Encoding.

One minor exception comes when generating CSV into a St...