Tbpgr Blog

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

Ruby | Module | public_method_defined?

概要

Module#public_method_defined?(name) -> bool

詳細

インスタンスメソッド name がモジュールに定義されており、 しかもその可視性が public であるときに true を返却。
そうでなければ false を返却。

サンプルコード
require 'tbpgr_utils'

module Hogeable
  def hogeable
  end

  protected

  def protected_hogeable
  end
end

class Hoge
  include Hogeable
  def hoge
    print 'hoge'
  end

  protected

  def protected_hoge
  end
end

class ChildHoge < Hoge
end

bulk_puts_eval binding, <<-EOS
Hoge.public_method_defined? :hogeable
Hoge.public_method_defined? :protected_hogeable
Hoge.public_method_defined? :hoge
Hoge.public_method_defined? :protected_hoge
Hoge.public_method_defined? 'protected_hoge'
ChildHoge.public_method_defined? :hoge
ChildHoge.public_method_defined? :protected_hogeable
ChildHoge.public_method_defined? :protected_hoge
Hogeable.public_method_defined? :protected_hogeable
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

Hoge.public_method_defined? :hogeable                # => true
Hoge.public_method_defined? :protected_hogeable      # => false
Hoge.public_method_defined? :hoge                    # => true
Hoge.public_method_defined? :protected_hoge          # => false
Hoge.public_method_defined? 'protected_hoge'         # => false
ChildHoge.public_method_defined? :hoge               # => true
ChildHoge.public_method_defined? :protected_hogeable # => false
ChildHoge.public_method_defined? :protected_hoge     # => false
Hogeable.public_method_defined? :hoge                # => false
Hogeable.public_method_defined? :protected_hogeable  # => false