Tbpgr Blog

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

Ruby | Method | receiver

概要

Method#receiver -> object

詳細

このメソッドオブジェクトのレシーバを返却。

サンプルコード
require 'tbpgr_utils'

class Hoge
  attr_reader :id

  def initialize(id)
    @id = id
  end

  def hoge; end
end

class Hige
  attr_reader :id

  def initialize(id)
    @id = id
  end

  def hoge; end
end

h1 = Hoge.new(1)
h2 = Hige.new(2)
bulk_puts_eval binding, <<-EOS
h1.method(:hoge).receiver
h2.method(:hoge).receiver
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

h1.method(:hoge).receiver # => #<Hoge:0x2596f98 @id=1>
h2.method(:hoge).receiver # => #<Hige:0x2596710 @id=2>