種類
- インスタンスメソッド (408)
- 特異メソッド (125)
- クラス (46)
- モジュール関数 (36)
ライブラリ
- ビルトイン (615)
クラス
- BasicObject (84)
- Class (48)
- Data (18)
- File (12)
- Module (24)
- Object (156)
- Proc (19)
- Struct (4)
- Thread (24)
-
Thread
:: Backtrace :: Location (48)
モジュール
- Enumerable (96)
- ObjectSpace (36)
キーワード
- ! (12)
- != (12)
- == (12)
- BasicObject (12)
- ConditionVariable (10)
- Location (12)
- RegexpError (12)
- [] (7)
-
_ dump (12) -
absolute
_ path (12) - allocate (12)
-
base
_ label (12) - clone (12)
- define (6)
-
define
_ finalizer (24) - dup (12)
- fork (12)
-
initialize
_ clone (12) -
initialize
_ copy (12) -
initialize
_ dup (12) - inspect (24)
-
instance
_ eval (24) -
instance
_ exec (12) -
instance
_ variable _ defined? (12) -
instance
_ variable _ get (12) -
marshal
_ dump (12) - max (48)
-
method
_ missing (12) - min (48)
- new (88)
- path (12)
-
singleton
_ method (12) - start (12)
-
to
_ s (24) -
undefine
_ finalizer (12)
検索結果
先頭5件
-
ObjectSpace
. # define _ finalizer(obj) {|id| . . . } -> Array (8018.0) -
obj が解放されるときに実行されるファイナライザ proc を 登録します。同じオブジェクトについて複数回呼ばれたときは置き換えで はなく追加登録されます。固定値 0 と proc を配列にして返します。
...れます。
=== 使い方の注意
以下は、define_finalizer の使い方の悪い例です。
//emlist[悪い例][ruby]{
class Foo
def initialize
ObjectSpace.define_finalizer(self) {
puts "foo"
}
end
end
Foo.new
GC.start
//}
これは、渡された proc の self が obj......とで上記の問題を回避しています。
//emlist[例][ruby]{
class Bar
def Bar.callback
proc {
puts "bar"
}
end
def initialize
ObjectSpace.define_finalizer(self, Bar.callback)
end
end
Bar.new
GC.start
//}
proc の呼び出しで発生した大域脱出(exitや例......-d オプションで
事前に例外の発生の有無を確認しておいた方が良いでしょう。
//emlist[例][ruby]{
class Baz
def initialize
ObjectSpace.define_finalizer self, eval(%q{
proc {
raise "baz" rescue puts $!
raise "baz2"
puts "baz3"... -
ObjectSpace
. # define _ finalizer(obj , proc) -> Array (8018.0) -
obj が解放されるときに実行されるファイナライザ proc を 登録します。同じオブジェクトについて複数回呼ばれたときは置き換えで はなく追加登録されます。固定値 0 と proc を配列にして返します。
...れます。
=== 使い方の注意
以下は、define_finalizer の使い方の悪い例です。
//emlist[悪い例][ruby]{
class Foo
def initialize
ObjectSpace.define_finalizer(self) {
puts "foo"
}
end
end
Foo.new
GC.start
//}
これは、渡された proc の self が obj......とで上記の問題を回避しています。
//emlist[例][ruby]{
class Bar
def Bar.callback
proc {
puts "bar"
}
end
def initialize
ObjectSpace.define_finalizer(self, Bar.callback)
end
end
Bar.new
GC.start
//}
proc の呼び出しで発生した大域脱出(exitや例......-d オプションで
事前に例外の発生の有無を確認しておいた方が良いでしょう。
//emlist[例][ruby]{
class Baz
def initialize
ObjectSpace.define_finalizer self, eval(%q{
proc {
raise "baz" rescue puts $!
raise "baz2"
puts "baz3"... -
Data
. define(*args) -> Class (8012.0) -
Data クラスに新しいサブクラスを作って、それを返します。
...を生成する際にオプション引数を使用したいときは、
initialize メソッドをオーバーライドすることで実現できます。
//emlist[例][ruby]{
Point = Data.define(:x, :y) do
def initialize(x:, y: 0)
super
end
end
p Point.new(x: 1) # => #<data Point... -
Data
. define(*args) {|subclass| block } -> Class (8012.0) -
Data クラスに新しいサブクラスを作って、それを返します。
...を生成する際にオプション引数を使用したいときは、
initialize メソッドをオーバーライドすることで実現できます。
//emlist[例][ruby]{
Point = Data.define(:x, :y) do
def initialize(x:, y: 0)
super
end
end
p Point.new(x: 1) # => #<data Point... -
Thread
:: Backtrace :: Location (8012.0) -
Ruby のフレームを表すクラスです。
...][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>'
=== 参考... -
Thread
:: Backtrace :: Location # base _ label -> String (8012.0) -
self が表すフレームの基本ラベルを返します。通常、 Thread::Backtrace::Location#label から修飾を取り除いたもので構成 されます。
...されます。
//emlist[例][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.base_label
end
# => initialize
# new
# <main>
//}
@see Thread::Backtrace::Location#label... -
Thread
:: Backtrace :: Location # inspect -> String (8012.0) -
Thread::Backtrace::Location#to_s の結果を人間が読みやすいような文 字列に変換したオブジェクトを返します。
...st[例][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.inspect
end
# => "path/to/foo.rb:5:in `initialize'"
# "path/to/foo.rb:9:in `new'"
# "path/to/foo.rb:9:in `<main>'"... -
Thread
:: Backtrace :: Location # to _ s -> String (8012.0) -
self が表すフレームを Kernel.#caller と同じ表現にした文字列を返し ます。
...mlist[例][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
# => path/to/foo.rb:5:in `initialize'
# path/to/foo.rb:9:in `new'
# path/to/foo.rb:9:in `<main>'
//}... -
BasicObject (8006.0)
-
特殊な用途のために意図的にほとんど何も定義されていないクラスです。 Objectクラスの親にあたります。Ruby 1.9 以降で導入されました。
...。
真に必要な場合にだけ BasicObject から派生してください。
=== 例
//emlist[例][ruby]{
class Proxy < BasicObject
def initialize(target)
@target = target
end
def method_missing(message, *args)
@target.__send__(message, *args)
end
end
proxy = Proxy.new("1")... -
BasicObject
# ! -> bool (8006.0) -
オブジェクトを真偽値として評価し、その論理否定を返します。
...せん。
@return オブジェクトが偽であれば真、さもなくば偽
//emlist[例][ruby]{
class NegationRecorder < BasicObject
def initialize
@count = 0
end
attr_reader :count
def !
@count += 1
super
end
end
recorder = NegationRecorder.new
!recorder
!!!!!!!record... -
BasicObject
# !=(other) -> bool (8006.0) -
オブジェクトが other と等しくないことを判定します。
...er 比較対象となるオブジェクト
@see BasicObject#==, BasicObject#!
//emlist[例][ruby]{
class NonequalityRecorder < BasicObject
def initialize
@count = 0
end
attr_reader :count
def !=(other)
@count += 1
super
end
end
recorder = NonequalityRecorder.new
recorder !=...