Fastest/One-liner way to list attr_accessors in Ruby?

前端 未结 6 838
忘了有多久
忘了有多久 2020-12-02 14:16

What\'s the shortest, one-liner way to list all methods defined with attr_accessor? I would like to make it so, if I have a class MyBaseClass, any

6条回答
  •  甜味超标
    2020-12-02 15:20

    It could be filtered with the equal sign matching logic:

    methods = (SubClass.instance_methods - SubClass.superclass.instance_methods).map(&:to_s)
    methods.select { |method| !method.end_with?('=') && methods.include?(method + '=') }
    #> ["id", "title", "body"]
    

提交回复
热议问题