Tbpgr Blog

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

Ruby | Module | protected_instance_methods

概要

Module#protected_instance_methods(inherited_too = true) -> [Symbol]

詳細

そのモジュールで定義されている protected メソッド名 の一覧を配列で返却。

サンプルコード
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.protected_instance_methods.first(5)
ChildHoge.protected_instance_methods.first(5)
Hogeable.protected_instance_methods.first(5)
Hoge.protected_instance_methods false
ChildHoge.protected_instance_methods false
Hogeable.protected_instance_methods false
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

出力

Hoge.protected_instance_methods.first(5)      # => [:protected_hoge, :protected_hogeable]
ChildHoge.protected_instance_methods.first(5) # => [:protected_hoge, :protected_hogeable]
Hogeable.protected_instance_methods.first(5)  # => [:protected_hogeable]
Hoge.protected_instance_methods false         # => [:protected_hoge]
ChildHoge.protected_instance_methods false    # => []
Hogeable.protected_instance_methods false     # => [:protected_hogeable]