36件ヒット
[1-36件を表示]
(0.052秒)
別のキーワード
ライブラリ
- ビルトイン (36)
キーワード
- Enumerator (12)
- Fiber (12)
- Location (12)
検索結果
-
Thread
:: Backtrace :: Location (41.0) -
Ruby のフレームを表すクラスです。
...Ruby のフレームを表すクラスです。
Kernel.#caller_locations から生成されます。
//emlist[例1][ruby]{
# caller_locations.rb
def a(skip)
caller_locations(skip)
end
def b(skip)
a(skip)
end
def c(skip)
b(skip)
end
c(0..2).map do |call|
puts call.to_s
end
//}
例1の実......st[例2][ruby]{
# foo.rb
class Foo
attr_accessor :locations
def initialize(skip)
@locations = caller_locations(skip)
end
end
Foo.new(0..2).locations.map do |call|
puts call.to_s
end
//}
例2の実行結果:
init.rb:4:in `initialize'
init.rb:8:in `new'
init.rb:8:in `<main>'
===......参考
* Ruby VM アドベントカレンダー #4 vm_backtrace.c: https://www.atdot.net/~ko1/diary/201212.html#d4... -
Fiber (37.0)
-
ノンプリエンプティブな軽量スレッド(以下ファイバーと呼ぶ)を提供します。 他の言語では coroutine あるいは semicoroutine と呼ばれることもあります。 Thread と違いユーザレベルスレッドとして実装されています。
...きます。
=== 例外
ファイバー実行中に例外が発生した場合、親ファイバーに例外が伝播します。
//emlist[例:][ruby]{
f = Fiber.new do
raise StandardError, "hoge"
end
begin
f.resume # ここでも StandardError が発生する。
rescue => e
p e.message #......と親にコンテキストを切り替えます。
Fiber.yield の引数が、親での Fiber#resume の返り値になります。
//emlist[例:][ruby]{
f = Fiber.new do
n = 0
loop do
Fiber.yield(n)
n += 1
end
end
5.times do
p f.resume
end
#=> 0
1
2
3
4
//}
以下......テレータを外部イテレータに変換する例です。
実際 Enumerator は Fiber を用いて実装されています。
//emlist[例:][ruby]{
def enum2gen(enum)
Fiber.new do
enum.each{|i|
Fiber.yield(i)
}
end
end
g = enum2gen(1..100)
p g.resume #=> 1
p g.resume #......ストを切り替えた時点で解消されます。
ファイバーが終了するとその親にコンテキストが切り替わります。
Ruby 3.1 から fiber を require しなくても、
コンテキストの切り替えに制限のない Fiber#transfer が使えます。
任意のフ... -
Enumerator (13.0)
-
each 以外のメソッドにも Enumerable の機能を提供するためのラッパークラスです。 また、外部イテレータとしても使えます。
...ます。
例えば以下のようなスレッドをまたいだ呼び出しはエラーになります。
//emlist[例][ruby]{
a = nil
Thread.new do
a = [1, 2, 3].each
a.next
end.join
p a.next
#=> t.rb:7:in `next': fiber called across threads (FiberError)
# from t.rb:7:in `<main>'
//}...