Tbpgr Blog

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

Ruby | Object | protected_methods

概要

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

詳細

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

サンプルコード
require 'tbpgr_utils'

module Mod
  protected
  def mod; end
end

class Parent
  protected
  def parent;end
end

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

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

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

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