LoginSignup
4
4

More than 5 years have passed since last update.

オブジェクトのインスタンスメソッドを調べる

Posted at

RubyのことはRubyに聞け

class Object
  def tell_me_methods
    self.class.instance_methods(false).map{|m|
      res = begin
        send(m)
      rescue=>e
        e.message
      end

      [ m, res ]
    }.sort_by{|k,v| v.class.to_s }.to_h
  end
end

1.tell_me_methods

{:even?=>false,
 :zero?=>false,
 :size=>8,
 :succ=>2,
 :bit_length=>1,
 :-@=>-1,
 :magnitude=>1,
 :~=>-2,
 :abs=>1,
 :to_f=>1.0,
 :>=>"wrong number of arguments (0 for 1)",
 :fdiv=>"wrong number of arguments (0 for 1)",
 :**=>"wrong number of arguments (0 for 1)",
 :modulo=>"wrong number of arguments (0 for 1)",
 :%=>"wrong number of arguments (0 for 1)",
 :===>"wrong number of arguments (0 for 1)",
 :====>"wrong number of arguments (0 for 1)",
 :<=>=>"wrong number of arguments (0 for 1)",
 :divmod=>"wrong number of arguments (0 for 1)",
 :>==>"wrong number of arguments (0 for 1)",
 :<=>"wrong number of arguments (0 for 1)",
 :<==>"wrong number of arguments (0 for 1)",
 :div=>"wrong number of arguments (0 for 1)",
 :inspect=>"1",
 :|=>"wrong number of arguments (0 for 1)",
 :^=>"wrong number of arguments (0 for 1)",
 :[]=>"wrong number of arguments (0 for 1)",
 :<<=>"wrong number of arguments (0 for 1)",
 :>>=>"wrong number of arguments (0 for 1)",
 :/=>"wrong number of arguments (0 for 1)",
 :*=>"wrong number of arguments (0 for 1)",
 :-=>"wrong number of arguments (0 for 1)",
 :+=>"wrong number of arguments (0 for 1)",
 :to_s=>"1",
 :&=>"wrong number of arguments (0 for 1)",
 :odd?=>true}

wrong number of argumentsは自分で叩くしかない(汗

4
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4