Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print all method names of a class in Ruby? [duplicate]

Tags:

ruby

If I want to list all methods for a class I can use Class_Name.methods, so, for example, to list all methods in the FixNum class:

> 5.methods  => [:to_s, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^, :[], :<<, :>>, :to_f, :size, :zero?, :odd?, :even?, :succ, :integer?, :upto, :downto, :times, :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, :remainder, :real?, :nonzero?, :step, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :between?, :nil?, :=~, :!~, :hash, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__] 

This list is really hard to read. I also tried 5.methods.sort but that does not make it more readable.

I frequently use the list of methods during my everyday programming. Is there a way to pretty-print this, so it becomes easier to read?

like image 711
CuriousMind Avatar asked Apr 23 '12 15:04

CuriousMind


1 Answers

Try this one-liner:

puts 5.methods.sort 
like image 51
seph Avatar answered Oct 16 '22 09:10

seph