I’m creating a class that creates arrays. (Yes, I realize that’s already available in ruby. This is more of an exercise.) The class, SpecialArray, creates an array based on input that’s either given by the user when creating a new array or added to the array later. I’m having trouble connecting these two pieces of functionality. Creating an array works fine, but adding to it does not. I had thought that the output atrr_reader would hold the exiting array in memory, but now I’m not sure. Any idea how I can make this work?
class SpecialArray
attr_reader :input, :output
def initialize(*input)
@input = input.flatten
@output = []
generate_array
end
def generate_array
input.each do |e|
add(e)
end
output
end
#update start
def numerical(element)
element.class == Fixnum
end
def unique(element)
output.include? element ? false : true
end
def valid_e(element)
unique(element) && numerical(element)
end
def numerical(elment)
element.class == Fixnum
end
def unique(element)
output.include? element ? false : true
end
def valid_e(element)
unique(element) && numerical(element)
end
#update end
def add(element)
unless valid_e(element) == false
output.push(element)
end
output
end
end
Seems to work fine to me, both
@inputand@outputcontain the array I passed in:EDIT Full Code