Search code examples
rubyclojurefunctional-programming

Clojure equivalent of ruby's #methods method?


In ruby, we can do Object.methods to get all the methods for a specific Object or class.

For instance:

irb(main):001:0> Object.methods
=> [:new, :allocate, :superclass, :<=>, :include, :<=, :>=, :==, :===, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :public_constant, :private_constant, :deprecate_constant, :singleton_class?, :module_exec, :class_exec, :freeze, :inspect, :const_missing, :class_eval, :method_defined?, :public_method_defined?, :prepend, :<, :>, :private_method_defined?, :protected_method_defined?, :public_class_method, :module_eval, :to_s, :private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :remove_instance_variable, :instance_variable_set, :method, :public_method, :singleton_method, :extend, :define_singleton_method, :to_enum, :enum_for, :=~, :!~, :eql?, :respond_to?, :object_id, :send, :display, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variable_get, :instance_variables, :instance_variable_defined?, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]

Can we do something similar in the Clojure repl (eg: leiningen)?


Solution

  • How to list the functions of a namespace? this is a similar question, please go to the link for the full explanation.

    In Clojure we don't have classes but functions are grouped into name spaces. using ns-publics you can get a map of all the functions in that namespace.

    (keys (ns-publics 'user))
    ; => (clojuredocs help find-name user.proxy$java.lang.Object$SignalHandler$d8c00ec7 cdoc apropos-better)