< up >
2024-03-18

ruby - get plain class/instance method list

tl;dr: <your-class>.methods - Object.methods

Whenever I juggle with ruby classes or instances within a ruby REPL1, I had hard times to figure out their methods:

> Time.methods
=> [:at,
 :now,
 :utc,
 :gm,
 :local,
 :mktime,
 :new,
 :allocate,
 :superclass,
 :<=>,
 :<=,
 :>=,
 :==,
 :===,
 :included_modules,
 :include?,
 :ancestors,
 :attr,
 :attr_reader,
 :attr_writer,
 :attr_accessor,
 :freeze,
 :inspect,
 :public_instance_methods,
 :instance_methods,
 :const_missing,
 :protected_instance_methods,
 :private_instance_methods,
 :const_set,
 :constants,
 :remove_class_variable,
 :class_variable_get,
 :class_variable_set,
 :class_variable_defined?,
 :const_get,
 :const_defined?,
 :<,
 :>,
 :public_constant,
 :class_variables,
 :private_constant,
 :deprecate_constant,
 :singleton_class?,
 :const_source_location,
 :to_s,
 :class_eval,
 :include,
 :module_exec,
 :module_eval,
 :prepend,
 :undef_method,
 :alias_method,
 :class_exec,
 :remove_method,
 :method_defined?,
 :name,
 :private_class_method,
 :public_method_defined?,
 :private_method_defined?,
 :protected_method_defined?,
 :public_class_method,
 :pretty_print_cycle,
 :instance_method,
 :public_instance_method,
 :define_method,
 :autoload,
 :autoload?,
 :pretty_print,
 :__binding__,
 :pry,
 :pretty_print_inspect,
 :pretty_print_instance_variables,
 :dup,
 :itself,
 :yield_self,
 :then,
 :taint,
 :tainted?,
 :untaint,
 :untrust,
 :untrusted?,
 :trust,
 :frozen?,
 :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,
 :class,
 :display,
 :hash,
 :singleton_class,
 :clone,
 :public_send,
 :method,
 :public_method,
 :singleton_method,
 :define_singleton_method,
 :pretty_inspect,
 :extend,
 :to_enum,
 :enum_for,
 :=~,
 :!~,
 :nil?,
 :eql?,
 :respond_to?,
 :object_id,
 :send,
 :__send__,
 :!,
 :!=,
 :equal?,
 :__id__,
 :instance_eval,
 :instance_exec]

I was today years old when I leaned about just subtracting the Object methods (from ruby’s holy base object) from SO:

pry(main)> Time.methods - Object.methods
=> [:at, :now, :utc, :gm, :local, :mktime]

As of writing this post, I just noticed that the relevant class/instance methods are at the beginning of the list, which is kind of handy, too.


  1. pry ftw.