I came across this problem on an online test. I have this class:
class DocumentStore
def initialize(capacity)
@capacity = capacity;
@documents = []
end
def get_documents
return @documents
end
def add_document(document)
raise 'Document store is full' if @documents.length >= @capacity
@documents.push(document)
end
def inspect
return "Document store: #{@documents.length}/#{@capacity}"
end
end
I want to return the store data via get_documents, and prevent the user from changing/affecting its value via the returned object, e.g.,
ds = DocumentStore.new(3)
ds.add_document("Doc1")
docs = ds.get_documents
docs.push("Doc2")
puts ds.inspect # this should just print ["Doc1"]