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

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?

Best way to retrieve Redis list values in Ruby?

What is the most common way is to retrieve all values in a list when those values are relatively large serialised Ruby objects? For example: I have 5000 values in a Redis list Each value in the list

ruby net sftp : fastest way to get list of files

I am using ruby Net-sftp gem,I need to download large number of small files before I download I need to make sure to get a list of files in the given directory. In order to do that I am using sftp.dir

More efficient way to create a list in Ruby

Just working with the 2-9 in a deck of cards. What is the preferred and/or most efficient way to get the deck created in Ruby? Is it what I have below, or how else can/should I do it? deck = [] suits

Cleanest way to approximately match strings to a list and return values in Ruby?

I want to build a function in Ruby that takes in a string and compares it to a list of possibilities before returning some value or function. Here’s a (possibly naive) way it could be written as a cas

Ruby simple way to list the next n numbers

In ruby we can easily generate 1 to n numbers by lots of ways, like 1.upto(10).to_a [*1..10] But I want to find out the next n numbers from the given number. For example, if the given number is 250,

create a list box in ruby on rails

Is there a simple way to present values in a list box in Ruby on Rails ? (something like adding the html select tag multiple attribute in ruby)

How to make a word list from a text file in a more Ruby way?

I’d like to get a word list from a text file using Ruby. I found how to use regex to parse only words here, so I made a script like following: src = File.open(text.txt) word_list = [] src.each do |l

Ruby grocery list program

I am currently learning Ruby and I’m trying to write a simple Ruby grocery_list method. Here are the instructions: We want to write a program to help keep track of a grocery list. It takes a grocery i

ruby gem –pre list/install, weird error?

rubygems 1.7.2, ruby 1.8.7. linux. I thought this was the way to see what pre versions of a gem are available: gem list –pre –remote someGem When I try that though, I get this error message: ***

How to make a list in Ruby?

I’m new at Ruby, and failing to Google this easy question: What’s the normal way to obtain a list of numbers [1, 2, …, n] in Ruby? In Haskell I just type [1..n], and I’m sure that this is easy in Ru

Answers

There is no way (one-liner or otherwise) to list all methods defined by attr_accessor and only methods defined by attr_accessor without defining your own attr_accessor.

Here’s a solution that overrides attr_accessor in MyBaseClass to remember which methods have been created using attr_accessor:

class MyBaseClass
  def self.attr_accessor(*vars)
    @attributes ||= []
    @attributes.concat vars
    super(*vars)
  end

  def self.attributes
    @attributes
  end

  def attributes
    self.class.attributes
  end
end

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

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

Heres an alternative using a mixin rather than inheritance:

module TrackAttributes
  def attr_readers
    self.class.instance_variable_get('@attr_readers')
  end

  def attr_writers
    self.class.instance_variable_get('@attr_writers')
  end

  def attr_accessors
    self.class.instance_variable_get('@attr_accessors')
  end

  def self.included(klass)
    klass.send :define_singleton_method, :attr_reader, ->(*params) do
      @attr_readers ||= []
      @attr_readers.concat params
      super(*params)
    end

    klass.send :define_singleton_method, :attr_writer, ->(*params) do
      @attr_writers ||= []
      @attr_writers.concat params
      super(*params)
    end

    klass.send :define_singleton_method, :attr_accessor, ->(*params) do
      @attr_accessors ||= []
      @attr_accessors.concat params
      super(*params)
    end
  end
end

class MyClass
  include TrackAttributes

  attr_accessor :id, :title, :body
end

MyClass.new.attr_accessors #=> [:id, :title, :body]

Following up on Christian’s response, but modifying to use ActiveSupport::Concern…

module TrackAttributes
  extend ActiveSupport::Concern

  included do
    define_singleton_method(:attr_reader) do |*params|
      @attr_readers ||= []
      @attr_readers.concat params
      super(*params)
    end

    define_singleton_method(:attr_writer) do |*params|
      @attr_writers ||= []
      @attr_writers.concat params
      super(*params)
    end

    define_singleton_method(:attr_accessor) do |*params|
      @attr_accessors ||= []
      @attr_accessors.concat params
      super(*params)
    end
  end

  def attr_readers
    self.class.instance_variable_get('@attr_readers')
  end

  def attr_writers
    self.class.instance_variable_get('@attr_writers')
  end

  def attr_accessors
    self.class.instance_variable_get('@attr_accessors')
  end

end

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]

Class definitions

class MyBaseClass
  attr_writer :an_attr_writer
  attr_reader :an_attr_reader

  def instance_m
  end

  def self.class_m
  end
end

class SubClass < MyBaseClass
  attr_accessor :id

  def sub_instance_m
  end

  def self.class_sub_m
  end
end

Call as class methods

p SubClass.instance_methods - Object.methods    
p MyBaseClass.instance_methods - Object.methods

Call as instance methods

a = SubClass.new
b = MyBaseClass.new

p a.methods - Object.methods
p b.methods - Object.methods

Both will output the same

#=> [:id, :sub_instance_m, :id=, :an_attr_reader, :instance_m, :an_attr_writer=]
#=> [:an_attr_reader, :instance_m, :an_attr_writer=]

How to tell which are writer reader and accessor?

attr_accessor is both attr_writer and attr_reader

attr_reader outputs no = after the method name

attr_writer outputs an = after the method name

You can always use a regular expression to filter that output.