【问题标题】:Clojure equivalent of ruby's #methods method?Clojure 相当于 ruby​​ 的#methods 方法?
【发布时间】:2020-01-18 23:18:09
【问题描述】:

在 ruby​​ 中,我们可以使用Object.methods 来获取特定对象或类的所有方法。

例如:

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__]

我们可以在 Clojure repl 中做类似的事情吗(例如:leiningen)?

【问题讨论】:

    标签: ruby clojure functional-programming


    【解决方案1】:

    要获取 Java 对象的方法,您可以使用clojure.reflect

    (map (comp keyword :name) (:members (clojure.reflect/reflect Object))))
    ;; => (:getClass :wait :finalize :java.lang.Object :equals :notifyAll :hashCode :toString :registerNatives :clone :notify :wait :wait)
    

    更多建议请见How can I get the methods of a Java class from Clojure?

    【讨论】:

      【解决方案2】:

      How to list the functions of a namespace?这是一个类似的问题,请到链接查看完整说明。

      在 Clojure 中,我们没有类,但函数被分组到名称空间中。使用ns-publics,您可以获得该命名空间中所有函数的映射。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-21
        • 2011-04-18
        • 2020-04-15
        • 2012-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多