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

Posted by viatropos on Stack Overflow See other posts from Stack Overflow or by viatropos
Published on 2010-03-21T14:04:26Z Indexed on 2010/03/21 14:11 UTC
Read the original article Hit count: 219

Filed under:
|
|

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, anything that extends that, I can get the attr_accessor's defined in the subclasses. Something like this:

class MyBaseClass < Hash
  def attributes
    # ??
  end
end

class SubClass < MyBaseClass
  attr_accessor :id, :title, :body
end

puts SubClass.new.attributes.inspect #=> [id, title, body]

What about to display just the attr_reader and attr_writer definitions?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about one-liner