Ruby: instance methods

Want to see just the methods specific to your instance?

instance_methods returns all the methods belonging to your instance. This includes methods of its superclass, e.g.

> i = Integer

> i.instance_methods

 => [:integer?, :odd?, :even?, :upto, :downto, :times, :succ, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :singleton_method_added, :coerce, :i, :+@, :-@, :<=>, :eql?, :quo, :fdiv, :div, :divmod, :%, :modulo, :remainder, :abs, :magnitude, :real?, :zero?, :nonzero?, :step, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :==, :>, :>=, :<, :<=, :between?, :nil?, :===, :=~, :!~, :hash, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__] 

but you can rule out the superclass methods by passing in ‘false’ as a parameter to instance_methods. E.g.

> i.instance_methods(false)

 => [:integer?, :odd?, :even?, :upto, :downto, :times, :succ, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize] 

http://www.ruby-doc.org/core-2.0/Object.html#method-i-methods

Leave a Reply

Your email address will not be published. Required fields are marked *