Ruby 1.8.7 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > UnboundMethodクラス

class UnboundMethod

クラスの継承リスト: UnboundMethod < Object < Kernel

要約

レシーバを持たないメソッドを表すクラスです。 呼び出すためにはレシーバにバインドする必要があります。

Module#instance_methodMethod#unbind により生成し、後で UnboundMethod#bind によりレシーバを 割り当てた Method オブジェクトを作ることができます。

例: Method クラスの冒頭にある例を UnboundMethod で書くと以下のようになります。

class Foo
  def foo() "foo" end
  def bar() "bar" end
  def baz() "baz" end
end

# 任意のキーとメソッドの関係をハッシュに保持しておく
# レシーバの情報がここにはないことに注意
methods = {1 => Foo.instance_method(:foo),
           2 => Foo.instance_method(:bar),
           3 => Foo.instance_method(:baz)}

# キーを使って関連するメソッドを呼び出す
# レシーバは任意(Foo クラスのインスタンスでなければならない)
p methods[1].bind(Foo.new).call      # => "foo"
p methods[2].bind(Foo.new).call      # => "bar"
p methods[3].bind(Foo.new).call      # => "baz"

例: 以下はメソッドの再定義を UnboundMethod を使って行う方法です。普通は alias や super を使います。

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

インスタンスメソッド

定義 説明
self == other -> bool
self === other -> bool

自身と other が同じクラスあるいは同じモジュールの同じメソッドを表す場合に true を返します。そうでない場合に false を返します。

arity -> Integer

メソッドが受け付ける引数の数を返します。

bind(obj) -> Method

self を obj にバインドした Method オブジェクトを生成し返します。

eql?(other) -> bool
equal?(other) -> bool

指定された other が self 自身である場合のみ真を返します。 これは Object クラスで定義されたデフォルトの動作で す。

hash -> Integer

自身の Object#object_id を返します。これは Object クラスで定義されたデフォルトの動作です。

name -> String

このメソッドの名前を返します。

owner -> Class | Module

このメソッドが定義されている class か module を返します。

継承したメソッド

=~ __id__ __send__ _dump class clone dclone display enum_for extend freeze frozen? initialize initialize_copy inspect instance_eval instance_exec instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? marshal_dump marshal_load method method_missing methods nil? pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods public_methods remove_instance_variable respond_to? singleton_method_added singleton_method_removed singleton_method_undefined singleton_methods taint tainted? tap to_a to_ary to_hash to_int to_io to_proc to_regexp to_s to_str to_yaml to_yaml_properties to_yaml_style untaint .new