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

Extract the attributes in to an array, assign them to a constant, then splat them in to attr_accessor.

class SubClass < MyBaseClass
  ATTRS = [:id, :title, :body]
  attr_accessor(*ATTRS)
end

Now you can access them via the constant:

puts SubClass.ATTRS #=> [:id, :title, :body]

Leave a Comment