Tbpgr Blog

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

Ruby | Object | private_methods

概要

Object#private_methods(include_inherited = true) -> [Symbol]

詳細

private メソッド名の一覧を返却。
include_inherited に false を指定すると自クラスのメソッドのみを返却

サンプルコード
require 'tbpgr_utils'

module Mod
  private
  def mod; end
end

class Parent
  private
  def parent;end
end

class Child < Parent
  include Mod
  private
  def child;end
end

child = Child.new
bulk_puts_eval binding, <<-EOS
child.private_methods.first(5)
child.private_methods(false).first(5)
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

$ ruby private_methods.rb
child.private_methods.first(5)        # => [:child, :mod, :parent, :Digest, :DelegateClass]
child.private_methods(false).first(5) # => [:child]