Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Method | owner

概要

Method#owner -> Symbol

詳細

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

サンプルコード
require 'tbpgr_utils'

module Hogeable
  def hogeable; end
end

class Hoge
  include Hogeable
  def hoge; end
end

h = Hoge.new
bulk_puts_eval binding, <<-EOS
h.method(:hoge).owner
h.method(:hogeable).owner
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

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