種類
- モジュール関数 (88)
- インスタンスメソッド (88)
- クラス (29)
- 特異メソッド (11)
ライブラリ
- ビルトイン (216)
モジュール
- Kernel (88)
キーワード
- ConditionVariable (9)
- IO (11)
- SizedQueue (9)
- []= (11)
-
caller
_ locations (22) - join (22)
- readlines (44)
- select (22)
- spawn (44)
- value (11)
検索結果
先頭5件
-
Thread
# [](name) -> object | nil (29100.0) -
name に対応したスレッドに固有のデータを取り出します。 name に対応するスレッド固有データがなければ nil を返し ます。
...Thread.new { Thread.current["name"] = "A" },
Thread.new { Thread.current[:name] = "B" },
Thread.new { Thread.current["name"] = "C" }
].each do |th|
th.join
puts "#{th.inspect}: #{th[:name]}"
end
# => #<Thread:0x00000002a54220 dead>: A
# => #<Thread:0x00000002a541a8 dead>: B
# => #<Thread:0......00002a54130 dead>: C
//}
Thread#[] と Thread#[]= を用いたスレッド固有の変数は
Fiber を切り替えると異なる変数を返す事に注意してください。
//emlist[][ruby]{
def meth(newvalue)
begin
oldvalue = Thread.current[:name]
Thread.current[:name] = newvalue......st[][ruby]{
f = Fiber.new {
meth(1) {
Fiber.yield
}
}
meth(2) {
f.resume
}
f.resume
p Thread.current[:name]
# => nil if fiber-local
# => 2 if thread-local (The value 2 is leaked to outside of meth method.)
//}
Fiber を切り替えても同じ変数を返したい場合は
Thread#thread_v... -
Thread
# []=(name , val) (17100.0) -
val を name に対応するスレッド固有のデータとして格納します。
...を文字列か Symbol で指定します。文字列を指定した場合は String#to_sym によりシンボルに変換されます。
@param val スレッド固有データを指定します。nil を指定するとそのスレッド固有データは削除されます。
@see Thread#[]......ーを文字列か Symbol で指定します。文字列を指定した場合は String#to_sym によりシンボルに変換されます。
@param val スレッド固有データを指定します。nil を指定するとそのスレッド固有データは削除されます。
@see Thread#[]... -
IO
# readlines(limit) -> [String] (14116.0) -
データを全て読み込んで、その各行を要素としてもつ配列を返します。 既に EOF に達していれば空配列 [] を返します。
...データを全て読み込んで、その各行を要素としてもつ配列を返します。
既に EOF に達していれば空配列 [] を返します。
テキスト読み込みメソッドとして動作します。
limit で最大読み込みバイト数を指定します。ただしマ......\nline3,\n")
File.open("testfile") { |f| p f.readlines } # => ["line1,\n", "line2,\n", "line3,\n"]
File.open("testfile") { |f| p f.readlines(3) } # => ["lin", "e1,", "\n", "lin", "e2,", "\n", "lin", "e3,", "\n"]
File.open("testfile") { |f| p f.readlines(",") } # => ["line1,", "\nline2,", "\nl... -
IO
# readlines(limit , chomp: false) -> [String] (14116.0) -
データを全て読み込んで、その各行を要素としてもつ配列を返します。 既に EOF に達していれば空配列 [] を返します。
...データを全て読み込んで、その各行を要素としてもつ配列を返します。
既に EOF に達していれば空配列 [] を返します。
テキスト読み込みメソッドとして動作します。
limit で最大読み込みバイト数を指定します。ただしマ......\nline3,\n")
File.open("testfile") { |f| p f.readlines } # => ["line1,\n", "line2,\n", "line3,\n"]
File.open("testfile") { |f| p f.readlines(3) } # => ["lin", "e1,", "\n", "lin", "e2,", "\n", "lin", "e3,", "\n"]
File.open("testfile") { |f| p f.readlines(",") } # => ["line1,", "\nline2,", "\nl......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 = $ / ) -> [String] (14116.0) -
データを全て読み込んで、その各行を要素としてもつ配列を返します。 既に EOF に達していれば空配列 [] を返します。
...データを全て読み込んで、その各行を要素としてもつ配列を返します。
既に EOF に達していれば空配列 [] を返します。
テキスト読み込みメソッドとして動作します。
limit で最大読み込みバイト数を指定します。ただしマ......\nline3,\n")
File.open("testfile") { |f| p f.readlines } # => ["line1,\n", "line2,\n", "line3,\n"]
File.open("testfile") { |f| p f.readlines(3) } # => ["lin", "e1,", "\n", "lin", "e2,", "\n", "lin", "e3,", "\n"]
File.open("testfile") { |f| p f.readlines(",") } # => ["line1,", "\nline2,", "\nl... -
IO
# readlines(rs = $ / , chomp: false) -> [String] (14116.0) -
データを全て読み込んで、その各行を要素としてもつ配列を返します。 既に EOF に達していれば空配列 [] を返します。
...データを全て読み込んで、その各行を要素としてもつ配列を返します。
既に EOF に達していれば空配列 [] を返します。
テキスト読み込みメソッドとして動作します。
limit で最大読み込みバイト数を指定します。ただしマ......\nline3,\n")
File.open("testfile") { |f| p f.readlines } # => ["line1,\n", "line2,\n", "line3,\n"]
File.open("testfile") { |f| p f.readlines(3) } # => ["lin", "e1,", "\n", "lin", "e2,", "\n", "lin", "e3,", "\n"]
File.open("testfile") { |f| p f.readlines(",") } # => ["line1,", "\nline2,", "\nl......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) -> [String] (14116.0) -
データを全て読み込んで、その各行を要素としてもつ配列を返します。 既に EOF に達していれば空配列 [] を返します。
...データを全て読み込んで、その各行を要素としてもつ配列を返します。
既に EOF に達していれば空配列 [] を返します。
テキスト読み込みメソッドとして動作します。
limit で最大読み込みバイト数を指定します。ただしマ......\nline3,\n")
File.open("testfile") { |f| p f.readlines } # => ["line1,\n", "line2,\n", "line3,\n"]
File.open("testfile") { |f| p f.readlines(3) } # => ["lin", "e1,", "\n", "lin", "e2,", "\n", "lin", "e3,", "\n"]
File.open("testfile") { |f| p f.readlines(",") } # => ["line1,", "\nline2,", "\nl... -
IO
# readlines(rs , limit , chomp: false) -> [String] (14116.0) -
データを全て読み込んで、その各行を要素としてもつ配列を返します。 既に EOF に達していれば空配列 [] を返します。
...データを全て読み込んで、その各行を要素としてもつ配列を返します。
既に EOF に達していれば空配列 [] を返します。
テキスト読み込みメソッドとして動作します。
limit で最大読み込みバイト数を指定します。ただしマ......\nline3,\n")
File.open("testfile") { |f| p f.readlines } # => ["line1,\n", "line2,\n", "line3,\n"]
File.open("testfile") { |f| p f.readlines(3) } # => ["lin", "e1,", "\n", "lin", "e2,", "\n", "lin", "e3,", "\n"]
File.open("testfile") { |f| p f.readlines(",") } # => ["line1,", "\nline2,", "\nl......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... -
Kernel
. # readlines(rs = $ / ) -> [String] (14106.0) -
ARGFを Kernel.#gets(rs) でEOFまで読み込んで、その各行を要素としてもつ配列を返します。 行の区切りは引数 rs で指定した文字列になります。
...ruby]{
ARGV << 'b.txt' << 'b.txt'
p readlines #=> ["hello\n", "it\n", "\n", "common\n", "hello\n", "it\n", "\n", "common\n"]
ARGV << 'b.txt' << 'b.txt'
p readlines(nil) #=> ["hello\nit\n\ncommon\n", "hello\nit\n\ncommon\n"]
ARGV << 'b.txt' << 'b.txt'
p readlines("") #=> ["hello\nit\n\n",......"common\n", "hello\nit\n\n", "common\n"]
ARGV << 'b.txt' << 'b.txt'
p readlines('it') #=> ["hello\nit", "\n\ncommon\n", "hello\nit", "\n\ncommon\n"]
p readlines #=> []
//}
//emlist[b.txt][ruby]{
hello
it
common
//}
@see $/,ARGF,Kernel.#gets, Kernel.#readline... -
Thread
:: ConditionVariable (11012.0) -
スレッドの同期機構の一つである状態変数を実現するクラスです。
...クラスです。
以下も ConditionVariable を理解するのに参考になります。
https://ruby-doc.com/docs/ProgrammingRuby/html/tut_threads.html#UF
=== Condition Variable とは
あるスレッド A が排他領域で動いていたとします。スレッド A は現在空いてい......ex = Mutex.new
cv = ConditionVariable.new
a = Thread.start {
mutex.synchronize {
...
while (条件が満たされない)
cv.wait(mutex)
end
...
}
}
b = Thread.start {
mutex.synchronize {
# 上の......Variable を使って wait しています。
require 'thread'
class TinyQueue
def initialize(max=2)
@max = max
@full = ConditionVariable.new
@empty = ConditionVariable.new
@mutex = Mutex.new
@q = []
end
def count
@q.size
end
def enq(v... -
Thread
# join -> self (11006.0) -
スレッド self の実行が終了するまで、カレントスレッドを停止し ます。self が例外により終了していれば、その例外がカレントス レッドに対して発生します。
...readError join を実行することによってデッドロックが起きる場合に発生します。またカレントスレッドを join したときにも発生します。
以下は、生成したすべてのスレッドの終了を待つ例です。
threads = []
threads.push(Thread.......new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.each {|t| t.join}... -
Thread
# join(limit) -> self | nil (11006.0) -
スレッド self の実行が終了するまで、カレントスレッドを停止し ます。self が例外により終了していれば、その例外がカレントス レッドに対して発生します。
...readError join を実行することによってデッドロックが起きる場合に発生します。またカレントスレッドを join したときにも発生します。
以下は、生成したすべてのスレッドの終了を待つ例です。
threads = []
threads.push(Thread.......new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.each {|t| t.join}... -
Thread
# value -> object (11006.0) -
スレッド self が終了するまで待ち(Thread#join と同じ)、 そのスレッドのブロックが返した値を返します。スレッド実行中に例外が 発生した場合には、その例外を再発生させます。
...ッド self が終了するまで待ち(Thread#join と同じ)、
そのスレッドのブロックが返した値を返します。スレッド実行中に例外が
発生した場合には、その例外を再発生させます。
スレッドが Thread#kill によって終了した場合は、......終了を待ち結果を出力する例です。
threads = []
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.push(Thread.new { n = rand(5); sleep n; n })
threads.each {|t| p t.value}
最後の行で、待ち合わ......せを行っていることがわかりにくいと思うなら以下
のように書くこともできます。
threads.each {|t| p t.join.value}...