LoginSignup
0
0

More than 1 year has passed since last update.

[Ruby]メソッドが定義されているクラスを調べる方法

Last updated at Posted at 2021-06-27

環境

Ruby 2.6.2

本題

Rubyではメソッドはクラスまたはモジュール内に定義されています。
呼び出したメソッドがどのクラスで定義されているか知りたいとき、はownerメソッドを使って調べることができます。

ownerメソッドはRubyのMethodクラスに定義されているインスタンスメソッドです。
メソッドを定義しているクラスかモジュールを返します。

実践

12.method(:to_i).owner #=> Integer

[1,2,3].method(:join).owner #=> Array

1.23456.method(:round).owner #=> Float

自作のクラス場合以下のようになります。

class Hoge
 def hoge

 end
end

h = Hoge.new

h.method(:hoge).owner
#=> Hoge

参考

リファレンスマニュアル

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0