Tbpgr Blog

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

書籍 Ruby Cookbook | オブジェクトのメソッドリスト

パンくず

Ruby Cookbook
オブジェクトのメソッドリスト

概要

オブジェクトのメソッドリスト

内容

メソッドリストの取得>
メソッドのリストの取得はObject#methods
クラスメソッドのリストの取得は Object#singleton_methods

メソッドの保持確認>
インスタンスメソッドの確認はrespond_to?
クラスメソッドの確認はmethod_defined?

サンプルコード

# encoding: Windows-31J
require "pp"

class Hoge
end

class ExtendedHoge < Hoge
  public
  def public_instance_hoge
  end
  
  protected
  def protected_instance_hoge
  end
  
  private
  def private_instance_hoge
  end
  
  def ExtendedHoge.class_hoge
  end
end

extended_hoge = ExtendedHoge.new
print "extended_hoge.methods="
pp extended_hoge.methods.sort

print "extended_hoge.public_methods="
pp extended_hoge.public_methods.sort

print "extended_hoge.protected_methods="
pp extended_hoge.protected_methods.sort

print "extended_hoge.private_methods="
pp extended_hoge.private_methods.sort

print "ExtendedHoge.singleton_methods="
pp ExtendedHoge.singleton_methods.sort

print "extended_hoge.respond_to? (:protected_instance_hoge)="
pp extended_hoge.respond_to? (:protected_instance_hoge)
print "ExtendedHoge.method_defined? (:singleton_methods)="
pp ExtendedHoge.method_defined? (:singleton_methods)

出力

extended_hoge.methods=[:!,
 :!=,
 :!~,
 :<=>,
 :==,
 :===,
 :=~,
 :__id__,
 :__send__,
 :class,
 :clone,
 :define_singleton_method,
 :display,
 :dup,
 :enum_for,
 :eql?,
 :equal?,
 :extend,
 :freeze,
 :frozen?,
 :hash,
 :initialize_clone,
 :initialize_dup,
 :inspect,
 :instance_eval,
 :instance_exec,
 :instance_of?,
 :instance_variable_defined?,
 :instance_variable_get,
 :instance_variable_set,
 :instance_variables,
 :is_a?,
 :kind_of?,
 :method,
 :methods,
 :nil?,
 :object_id,
 :pretty_inspect,
 :pretty_print,
 :pretty_print_cycle,
 :pretty_print_inspect,
 :pretty_print_instance_variables,
 :private_methods,
 :protected_instance_hoge,
 :protected_methods,
 :public_instance_hoge,
 :public_method,
 :public_methods,
 :public_send,
 :respond_to?,
 :respond_to_missing?,
 :send,
 :singleton_class,
 :singleton_methods,
 :taint,
 :tainted?,
 :tap,
 :to_enum,
 :to_s,
 :trust,
 :untaint,
 :untrust,
 :untrusted?]
extended_hoge.public_methods=[:!,
 :!=,
 :!~,
 :<=>,
 :==,
 :===,
 :=~,
 :__id__,
 :__send__,
 :class,
 :clone,
 :define_singleton_method,
 :display,
 :dup,
 :enum_for,
 :eql?,
 :equal?,
 :extend,
 :freeze,
 :frozen?,
 :hash,
 :initialize_clone,
 :initialize_dup,
 :inspect,
 :instance_eval,
 :instance_exec,
 :instance_of?,
 :instance_variable_defined?,
 :instance_variable_get,
 :instance_variable_set,
 :instance_variables,
 :is_a?,
 :kind_of?,
 :method,
 :methods,
 :nil?,
 :object_id,
 :pretty_inspect,
 :pretty_print,
 :pretty_print_cycle,
 :pretty_print_inspect,
 :pretty_print_instance_variables,
 :private_methods,
 :protected_methods,
 :public_instance_hoge,
 :public_method,
 :public_methods,
 :public_send,
 :respond_to?,
 :respond_to_missing?,
 :send,
 :singleton_class,
 :singleton_methods,
 :taint,
 :tainted?,
 :tap,
 :to_enum,
 :to_s,
 :trust,
 :untaint,
 :untrust,
 :untrusted?]
extended_hoge.protected_methods=[:protected_instance_hoge]
extended_hoge.private_methods=[:Array,
 :Complex,
 :Float,
 :Integer,
 :Rational,
 :String,
 :__callee__,
 :__method__,
 :`,
 :abort,
 :at_exit,
 :autoload,
 :autoload?,
 :binding,
 :block_given?,
 :caller,
 :catch,
 :eval,
 :exec,
 :exit,
 :exit!,
 :fail,
 :fork,
 :format,
 :gem,
 :gem_original_require,
 :gets,
 :global_variables,
 :initialize,
 :initialize_copy,
 :iterator?,
 :lambda,
 :load,
 :local_variables,
 :loop,
 :method_missing,
 :open,
 :p,
 :pp,
 :print,
 :printf,
 :private_instance_hoge,
 :proc,
 :putc,
 :puts,
 :raise,
 :rand,
 :readline,
 :readlines,
 :remove_instance_variable,
 :require,
 :require_relative,
 :select,
 :set_trace_func,
 :singleton_method_added,
 :singleton_method_removed,
 :singleton_method_undefined,
 :sleep,
 :spawn,
 :sprintf,
 :srand,
 :syscall,
 :system,
 :test,
 :throw,
 :trace_var,
 :trap,
 :untrace_var,
 :warn]
ExtendedHoge.singleton_methods=[:class_hoge]
extended_hoge.respond_to? (:protected_instance_hoge)=true
ExtendedHoge.method_defined? (:singleton_methods)=true