25

Suppose I have a class A

class A
  attr_accessor :x, :y

  def initialize(x,y)
    @x, @y = x, y
  end
end

How can I get x and y attributes without knowing how exactly they were named.

E.g.

a = A.new(5,10)
a.attributes # => [5, 10]
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
user1179942
  • 381
  • 1
  • 4
  • 9

6 Answers6

36

Use introspection, Luke!

class A
  attr_accessor :x, :y

  def initialize(*args)
    @x, @y = args
  end

  def attrs
    instance_variables.map{|ivar| instance_variable_get ivar}
  end
end

a = A.new(5,10)
a.x # => 5
a.y # => 10
a.attrs # => [5, 10]
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 6
    Note: attrs will return *all* instance variables, rather than just those exposed by `attr_accessor` – Jonah Dec 30 '15 at 17:26
  • 2
    @Jonah: yes, that was the assumption back then. For a more precise method, one can refer to [this answer](http://stackoverflow.com/a/34440466/125816). – Sergio Tulentsev Dec 30 '15 at 17:47
16

While Sergio's answer helps, it will return all the instance variables, which if I understand correctly the OP's question, is not what is asked.

If you want to return only the 'attributes' that have e.g. a mutator, you have to do something slightly more complicated such as:

attrs = Hash.new
instance_variables.each do |var|
  str = var.to_s.gsub /^@/, ''
  if respond_to? "#{str}="
    attrs[str.to_sym] = instance_variable_get var
  end
end
attrs

This returns only the attributes declared with attr_accessor (or with a manually created mutator), and keep the internal instance variables hidden. You can do something similar if you want the ones declared with attr_reader.

Nicolas Bonnefon
  • 537
  • 4
  • 12
  • I think this is a better answer given the question. I'll just append on to it. Ruby creates {Variable}= methods whenever a symbol has been added as an accessor or writer. respond_to? just ensures that there is a valid method that exist with the {Variable}= name. The first part is to chomp off the first "@" on the returned variable. In the OP example, methods "x=" and "y=" will get created, and you're looking for them with @x;x= and @y;y= – Keith D Ball Aug 14 '14 at 22:39
  • In some instances, this will include `:attributes=`. :/ – XtraSimplicity Nov 02 '17 at 08:17
15
class A
  ATTRIBUTES = [:x, :y]
  attr_accessor *ATTRIBUTES

  def initialize(x,y)
    @x, @y = x, y
  end

  def attributes
    ATTRIBUTES.map{|attribute| self.send(attribute) }
  end
end

This may not be the DRY-est, but if you are only concerned with doing this for one class (as opposed to a base class that everything inherits from), then this should work.

marksiemers
  • 631
  • 8
  • 14
3

See this other Stack Overflow Question. They override attr_accessor.

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

  def self.attributes
    @attributes
  end

  def attributes
    self.class.attributes
  end
reto
  • 16,189
  • 7
  • 53
  • 67
2

If you have attr_writers/attr_accessors defined on your attributes, than they can be easily retrieved by matching the =$ regexp:

A.instance_methods.each_with_object([]) { |key, acc| acc << key.to_s.gsub(/=$/, '') if key.match(/\w=$/) }

OR

A.instance_methods.each_with_object([]) { |key, acc| acc << key if key = key.to_s.match(/^(.*\w)=$/)&.[](1) }
zinovyev
  • 2,084
  • 1
  • 22
  • 32
1

when you use attr_accessor to define attributes in a class, Ruby using refexion, define a couple of methods, for each attribute declared, one to get the value and other to set, an instance variable of the same name of the attribute

you can see this methods using

p A.instance_methods

[:x, :x=, :y, :y=, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?,..

So this attributes are accesible, outside the class, with

p "#{a.x},#{a.y}"

or inside the class through the corresponding instance variable

class A
  ...
  def attributes
    [@x,@y]
  end
  ...
end
p a.attributes   #=> [5,10]
pgon
  • 55
  • 1
  • 3
    Thanks for your response, but I asked about a case when I don't know the names of the attributes, because otherwise it wouldn't be DRY approach. For example, what if I decide to add additional attributes `@d`, `@e`, `@f`. Then I'll have to add them in `attributes` method manualy as well as in `initialize`, which leads to the duplication of code. – user1179942 Apr 05 '12 at 14:17
  • In this situation, use the Sergio's solution – pgon Apr 05 '12 at 19:36