Tbpgr Blog

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

Ruby | Module | private_instance_methods

概要

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

詳細

そのモジュールで定義されている private メソッド名 の一覧を配列で返却。
inherited_too によって、継承元のメソッド名も取得するか変更可能。

サンプルコード
require 'tbpgr_utils'

module Hogeable
  def hogeable
  end

  private

  def private_hogeable
  end
end

class Hoge
  include Hogeable
  def hoge
    print 'hoge'
  end

  private

  def private_hoge
  end
end

class ChildHoge < Hoge
end

bulk_puts_eval binding, <<-EOS
Hoge.private_instance_methods.first(5)
ChildHoge.private_instance_methods.first(5)
Hogeable.private_instance_methods.first(5)
Hoge.private_instance_methods false
ChildHoge.private_instance_methods false
Hogeable.private_instance_methods false
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

出力

Hoge.private_instance_methods.first(5)      # => [:private_hoge, :private_hogeable, :Digest, :DelegateClass, :initialize_copy]
ChildHoge.private_instance_methods.first(5) # => [:private_hoge, :private_hogeable, :Digest, :DelegateClass, :initialize_copy]
Hogeable.private_instance_methods.first(5)  # => [:private_hogeable]
Hoge.private_instance_methods false         # => [:private_hoge]
ChildHoge.private_instance_methods false    # => []
Hogeable.private_instance_methods false     # => [:private_hogeable]