Tbpgr Blog

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

Ruby | Object | public_method

概要

Object#public_method(name) -> Method

詳細

オブジェクトの public メソッド name をオブジェクト化した Method オブジェクトを返却

サンプルコード
require 'tbpgr_utils'

module Mod
  def mod
    'mod'
  end
end

class Parent
  def parent
    'parent'
  end
end

class Child < Parent
  include Mod
  def child
    'child'
  end
end

child = Child.new
bulk_puts_eval binding, <<-EOS
child.public_method(:mod)
child.public_method(:mod).()
child.public_method(:parent)
child.public_method(:parent).()
child.public_method(:child)
child.public_method(:child).()
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

$ ruby public_method.rb
child.public_method(:mod)       # =>          #<Method: Child(Mod)   #mod>
child.public_method(:mod).()    # => "mod"
child.public_method(:parent)    # =>          #<Method: Child(Parent)#parent>
child.public_method(:parent).() # => "parent"
child.public_method(:child)     # =>          #<Method: Child        #child>
child.public_method(:child).()  # => "child"