Ruby class instance variables and inheritance
Solution 1
Since you mention that the attributes are "fixed" and "unchanging", I am assuming that you mean that you will never change their value once the object is created. In that case, something like the following should work:
class Foo
ATTRS = ['title', 'authors', 'location']
def attributes
ATTRS
end
end
class Bar < Foo
ATTRS = ['ISBN', 'pages']
def attributes
super + ATTRS
end
end
You are manually implementing a reader method (instead of letting attr_accessor
create it for you) that disguises the internal name of the array. In your subclass, you simply call the ancestor class' reader function, tack on the additional fields associated with the child class, and return that to the caller. To the user, this appears like a read-only member variable named attributes
that has additional values in the sub-class.
Solution 2
Another solution would be to use the inherited hook:
class LibraryItem < Object
class << self
attr_accessor :attributes
def inherit_attributes(attrs)
@attributes ||= []
@attributes.concat attrs
end
def inherited(sublass)
sublass.inherit_attributes(@attributes)
end
end
@attributes = ['title', 'authors', 'location',]
end
class LibraryBook < LibraryItem
@attributes.push('ISBN', 'pages')
end
Solution 3
Just as a version:
class LibraryItem < Object
def initialize
@attributes = ['one', 'two'];
end
end
class LibraryBook < LibraryItem
def initialize
super
@attributes.push('three')
end
end
b = LibraryBook.new
Solution 4
Out of curiosity, will something like this work?
class Foo
ATTRIBUTES = ['title','authors','location']
end
class Bar < Foo
ATTRIBUTES |= ['ISBN', 'pages']
end
This would seem to produce the desired result - the ATTRIBUTES array is expanded when the class object is created, and the values of ATTRIBUTES varies as expected:
> Foo::ATTRIBUTES
=> ['title','authors','location']
> Bar::ATTRIBUTES
=> ['title','authors','location', 'ISBN', 'pages']
Solution 5
To expand on @Nick Vanderbilt's answer, using active_support you do this, which is exactly the short hand I want for this functionality. Here's a complete example:
require 'active_support/core_ext'
class Foo
class_attribute :attributes
self.attributes = ['title','authors','location']
end
class Bar < Foo
self.attributes = Foo.attributes + ['ISBN', 'pages']
end
puts Foo.attributes.inspect #=> ["title", "authors", "location"]
puts Bar.attributes.inspect #=> ["title", "authors", "location", "ISBN", "pages"]
Shame it's so difficult for ruby to achieve this without needing a library for it. It's the only thing I miss from python. And in my case, I don't mind the dependency on the active_support gem.
rlandster
Updated on July 09, 2022Comments
-
rlandster almost 2 years
I have a Ruby class called
LibraryItem
. I want to associate with every instance of this class an array of attributes. This array is long and looks something like['title', 'authors', 'location', ...]
Note that these attributes are not really supposed to be methods, just a list of attributes that a
LibraryItem
has.Next, I want to make a subclass of
LibraryItem
calledLibraryBook
that has an array of attributes that includes all the attributes ofLibraryItem
but will also include many more.Eventually I will want several subclasses of
LibraryItem
each with their own version of the array@attributes
but each adding on toLibraryItem
's@attributes
(e.g.,LibraryBook
,LibraryDVD
,LibraryMap
, etc.).So, here is my attempt:
class LibraryItem < Object class << self; attr_accessor :attributes; end @attributes = ['title', 'authors', 'location',] end class LibraryBook < LibraryItem @attributes.push('ISBN', 'pages') end
This does not work. I get the error
undefined method `push' for nil:NilClass
If it were to work, I would want something like this
puts LibraryItem.attributes puts LibraryBook.attributes
to output
['title', 'authors', 'location'] ['title', 'authors', 'location', 'ISBN', 'pages']
(Added 02-May-2010) One solution to this is to make
@attributes
a simple instance variable and then add the new attributes forLibraryBoot
in theinitialize
method (this was suggested by demas in one of the answers).While this would certainly work (and is, in fact, what I have been doing all along), I am not happy with this as it is sub-optimal: why should these unchanging arrays be constructed every time an object is created?
What I really want is to have class variables that can inherit from a parent class but when changed in the child class do not change in the the parent class.
-
rlandster about 14 yearsThis is not what I want. I want the class instance variable for LibraryItem to contain only ['title', 'authors', 'location',] while the same instance variable for LibraryBook to contain ['title', 'authors', 'location',] plus ['ISBN', 'pages']. I will edit the question to make this clearer.
-
rlandster about 14 yearsThis is, in fact, how I am doing it now. But this solution is not optimal from a performance perspective. Setting the attributes in the initialize method means that attributes-setting code gets run for every object created. But the attributes are fixed, so, theoretically at least, there should be a way of setting the attributes just once at compile time for each class. I will rewrite my question (again) to make this clearer.
-
rlandster almost 14 yearsI want every instance of the class to have the specified attributes. Your suggestion defines constant arrays not object attributes.
-
Matt Connolly about 12 yearsIt should be
||=
instead of|=
. But even then it will fail because the ATTRIBUTES constant in Bar is not yet defined, so you cannot use||=
on it. -
Clint Pachl about 10 yearsThis has syntax errors. Plus, the class object attribute attributes isn't even connected to the ATTRIBUTES constant in anyway.
-
Clint Pachl about 10 years@MattConnolly, no it should not be
||=
.Array#|
is set union. In the example, Bar::ATTRIBUTES becomes the union of Foo::ATTRIBUTES and the two-element array literal defined in Bar.