Tbpgr Blog

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

Ruby | Module | public_instance_method

概要

Module#public_instance_method(name) -> UnboundMethod

詳細

self の public インスタンスメソッド name をオブジェクト化した UnboundMethod を返却する。

サンプルコード
require 'tbpgr_utils'

class Hoge
  def hoge
    'hoge'
  end
end

bulk_puts_eval binding, <<-EOS
Hoge.public_instance_method :hoge
Hoge.public_instance_method 'hoge'
Hoge.public_instance_method('hoge').bind(Hoge.new).call
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

出力

Hoge.public_instance_method :hoge                       # =>       #<UnboundMethod: Hoge#hoge>
Hoge.public_instance_method 'hoge'                      # =>       #<UnboundMethod: Hoge#hoge>
Hoge.public_instance_method('hoge').bind(Hoge.new).call # => "hoge"