I use my MOO project to teach myself Test Driven Design, and it takes me to interesting places. For example, I wrote a test that says that an attribute on a particular object should always return an array, so -
t = Thing.new("test")
p t.names
t.names = nil
p t.names
The code I have for this is fine, but it doesn’t seem terribly ruby to me:
class Thing
def initialize(names)
self.names = names
end
def names=(n)
n = [] if n.nil?
n = [n] unless n.instance_of?(Array)
@names = n
end
attr_reader :names
end
Is there a more elegant, Ruby-ish way to do this?
(NB: if someone wants to tell me why this is a dumb test for writing, that would be interesting too ...)
source
share