るりまサーチ

最速Rubyリファレンスマニュアル検索!
615件ヒット [101-200件を表示] (0.260秒)
トップページ > クエリ:_builtin[x] > クエリ:initialize[x]

別のキーワード

  1. _builtin new
  2. _builtin inspect
  3. _builtin []
  4. _builtin to_s
  5. _builtin each

ライブラリ

クラス

モジュール

キーワード

検索結果

<< < 1 2 3 4 ... > >>

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 !=...
<< < 1 2 3 4 ... > >>