96件ヒット
[1-96件を表示]
(0.017秒)
ライブラリ
- ビルトイン (60)
- continuation (12)
- fiddle (24)
キーワード
- BlockCaller (12)
- Closure (12)
- Continuation (12)
- Location (12)
- Method (12)
- Proc (12)
- SystemCallError (12)
- UnboundMethod (12)
検索結果
-
Fiddle
:: Closure :: BlockCaller (6007.0) -
Ruby のブロックをラップしたコールバック関数を表すクラスです。
...[TYPE_VOIDP, TYPE_INT, TYPE_INT, TYPE_VOIDP],
TYPE_VOID)
compare = Fiddle::Closure::BlockCaller.new(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP]){|x, y|
# qsort の比較関数は 型が int(*)(void*, void*) であるため、
# このブロ......は DL::CPtr オブジェクトが渡される。
# そのポインタが指す先は比較している文字なので、
# DL::CPtr#to_s で1文字の文字列に変換している
x.to_s(1) <=> y.to_s(1)
}
s = "7x0cba(Uq)"
qs.call(s, s.size, 1, compare)
p s # => "()07Uabcqx"... -
SystemCallError (6001.0)
-
Ruby の実装に用いられているシステムコールまたは一部の C 言語関数が失敗した時に発生する例外です。 システムコールの失敗した原因を表すエラーコードを保持します。
...る例外です。
システムコールの失敗した原因を表すエラーコードを保持します。
多くの場合、実際には SystemCallError そのものではなく
サブクラスである Errno::EXXX (XXX はエラーコードの値によって異なる。
システム定義の... -
Proc (61.0)
-
ブロックをコンテキスト(ローカル変数のスコープやスタックフ レーム)とともにオブジェクト化した手続きオブジェクトです。
...roc.new { var }
var = 2
def foo
$foo.call
end
p foo # => 2
//}
===[a:should_use_next] 手続きを中断して値を返す
手続きオブジェクトを中断して、呼出し元(呼び出しブロックでは yield、それ以外では Proc#call)
へジャンプし値を返すには n......せん。
//emlist[例][ruby]{
def foo
f = Proc.new{
next 1
2 # この行に到達することはない
}
end
p foo().call #=> 1
//}
===[a:block] Proc オブジェクトをブロック付きメソッド呼び出しに使う
ブロック付きメソッドに対し......y]{
b = Proc.new{|a,b,c|
p a,b,c
}
b.call(2, 4)
#=> 2
4
nil
//}
//emlist[lambda は引数の数が違うとエラーになる][ruby]{
b = lambda{|a,b,c|
p a,b,c
}
b.call(2, 4)
#=> wrong number of arguments (2 for 3) (ArgumentError)
//}
d:spec/call#block_arg も参照してください......uby]{
b = Proc.new{|a,b,c|
p a,b,c
}
b.call(2, 4)
#=> 2
4
nil
//}
//emlist[lambda は引数の数が違うとエラーになる][ruby]{
b = lambda{|a,b,c|
p a,b,c
}
b.call(2, 4)
# => wrong number of arguments (given 2, expected 3)
//}
d:spec/call#block_arg も参照してください... -
Continuation (31.0)
-
継続を表すクラスです。
...ラスです。
Kernel.#callcc { |cont| ... } の呼び出し
は、直前の状態(ローカル変数の定義、スタックフレーム)を cont に記憶
してブロックを実行します。cont は、Continuation クラスのインスタ
ンスで、Continuation#call メソッドを実行......p() == callcc {|c| }
longjmp() == c.call
と考えれば、わかりやすいかも知れません(ただし、callcc はスタックが深く
なる方向にもジャンプ出来るという違いがあります)
callcc() は、ブロックの戻り値を返しますが、Continuation#call(args)......:
以下は、Continuationによる無限ループの例
def LOOP
c = nil
yield callcc {|cnt| c = cnt; true }
c.call(false)
end
LOOP {|v| p v}
=> true
false
false
false
:
:
callcc とは、call-with-current-continuation の略です。... -
Fiddle
:: Closure (31.0) -
コールバック関数を表すクラスです。
...ラスです。
Ruby のメソッド(call)を C の関数ポインタとして表現するためのクラスです。
FFI の closure の wrapper です。
利用法としては、このクラスのサブクラスを作って
そのサブクラスに call メソッドを定義し、
new でオブ......ジェクトを生成することで利用します。
require 'fiddle'
include Fiddle # TYPE_* を使うために include する
class Compare < Fiddle::Closure
# qsort の比較関数は 型が int(*)(void*, void*) であるため、
# このメソッドには DL::CPtr オブ......def call(x, y)
x.to_s(1) <=> y.to_s(1)
end
end
libc = DL.dlopen("/lib/libc.so.6")
qs = Fiddle::Function.new(libc["qsort"],
[TYPE_VOIDP, TYPE_INT, TYPE_INT, TYPE_VOIDP],
TYPE_VOID)
s = "7x0cba(Uq)"
qs.call(s, s.size,... -
Method (31.0)
-
Object#method によりオブジェクト化され たメソッドオブジェクトのクラスです。
...ドを Method オブジェクト化する。
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo)
p m # => #<Method: Foo#foo>
p m.call(1) # => "foo called with arg 1"
//}
名前のないメソッド(の代わり)が必......t[例][ruby]{
pr = Proc.new {|arg|
"proc called with arg #{arg}"
}
p pr # => #<Proc:0x401b1fcc>
p pr.call(1) # => "proc called with arg 1"
//}
Method オブジェクトが有用なのは以下のような場合。
//emlist[例][ruby]{
class Foo
def foo() "foo" end
def bar() "b......[1].call # => "foo"
p methods[2].call # => "bar"
p methods[3].call # => "baz"
//}
しかし、レシーバを固定させる(Method オブジェクトはレシーバを保持する)必
要がないなら Object#public_sendを使う方法も有用。
//emlist[例][ruby]{
class Foo... -
Thread
:: Backtrace :: Location (25.0) -
Ruby のフレームを表すクラスです。
...nel.#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の実行結果:
caller_locations.rb:2:in `a'
caller_l......ocations.rb:5:in `b'
caller_locations.rb:8:in `c'
//emlist[例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 `ini... -
UnboundMethod (25.0)
-
レシーバを持たないメソッドを表すクラスです。 呼び出すためにはレシーバにバインドする必要があります。
...とができます。
//emlist[例: Method クラスの冒頭にある例を UnboundMethod で書くと以下のようになります。][ruby]{
class Foo
def foo() "foo" end
def bar() "bar" end
def baz() "baz" end
end
# 任意のキーとメソッドの関係をハッシュに保持して......d(Foo.new).call # => "foo"
p methods[2].bind(Foo.new).call # => "bar"
p methods[3].bind(Foo.new).call # => "baz"
//}
//emlist[例: 以下はメソッドの再定義を UnboundMethod を使って行う方法です。普通は alias や super を使います。][ruby]{
class Foo
def......foo
p :foo
end
@@orig_foo = instance_method :foo
def foo
p :bar
@@orig_foo.bind(self).call
end
end
Foo.new.foo
# => :bar
# :foo
//}...