Tbpgr Blog

元エンジニア 人事 tbpgr(てぃーびー) のブログ

Ruby | Object | singleton_methods

概要

Object#singleton_methods(inherited_too = true) -> [Symbol]

詳細

そのオブジェクトに対して定義されている特異メソッド名 (public あるいは protected メソッド) の一覧を返却。
inherited_too が true の場合、継承したメソッドも含む。

サンプルコード
require 'tbpgr_utils'

class ParentHoge
  class << self
    def public_class_parent_hoge; end
    protected
    def protected_class_parent_hoge; end
    private
    def private_class_parent_hoge; end
  end

  def public_parent_hoge; end
  protected
  def protected_parent_hoge; end
  private
  def private_parent_hoge; end
end

module Hogeable
  def public_hogeable; end
  protected
  def protected_hogeable; end
  private
  def private_hogeable; end
end

class Hoge < ParentHoge
  class << self
    def public_class_hoge; end
    protected
    def protected_class_hoge; end
    private
    def private_class_hoge; end
  end

  def public_hoge; end
  protected
  def protected_hoge; end
  private
  def private_hoge; end
end

hoge = Hoge.new
class << hoge
  def public_singleton_hoge; end
  protected
  def protected_singleton_hoge; end
  private
  def private_singleton_hoge; end
end

hoge.extend Hogeable

bulk_puts_eval binding, <<-EOS
hoge.methods.grep(/hoge/) - hoge.singleton_methods.grep(/hoge/)
hoge.singleton_methods.grep(/hoge/)
Hoge.methods.grep(/hoge/) - Hoge.singleton_methods.grep(/hoge/)
Hoge.singleton_methods.grep(/hoge/)
hoge.singleton_methods(false).grep(/hoge/)
Hoge.singleton_methods(false).grep(/hoge/)
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

hoge.methods.grep(/hoge/) - hoge.singleton_methods.grep(/hoge/) # => [:public_hoge, :protected_hoge, :public_parent_hoge, :protected_parent_hoge]
hoge.singleton_methods.grep(/hoge/)                             # => [:public_singleton_hoge, :protected_singleton_hoge, :public_hogeable, :protected_hogeable]
Hoge.methods.grep(/hoge/) - Hoge.singleton_methods.grep(/hoge/) # => []
Hoge.singleton_methods.grep(/hoge/)                             # => [:public_class_hoge, :protected_class_hoge, :public_class_parent_hoge, :protected_class_parent_hoge]
hoge.singleton_methods(false).grep(/hoge/)                      # => [:public_singleton_hoge, :protected_singleton_hoge]
Hoge.singleton_methods(false).grep(/hoge/)                      # => [:public_class_hoge, :protected_class_hoge]