Skip to content

Instantly share code, notes, and snippets.

@ralphos
Created June 29, 2012 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ralphos/3018324 to your computer and use it in GitHub Desktop.
Save ralphos/3018324 to your computer and use it in GitHub Desktop.
UniqueArray Problem
For this problem you will be creating a Ruby class which behaves exactly like an array but will store only numbers, will store them in the order they were added and when adding new elements will ignore any duplicates. In this way it functions like a Ruby hash object in that each key has to be unique.
Here's an example:
# Creating an array from another array. Notice that only the first occurrence of the number 2 is kept
UniqueArray.new([1,2,0,6,2,11])
=> [1,2,0,6,11]
# Creating an array by adding one element at a time
m = UniqueArray.new()
[1,2,0,6,2,11].each do |new_element|
m.add(new_element)
end
puts m
=> [1,2,0,6,11]
Submit your ruby file containing the class you wrote. Also submit a test file which demonstrates how your class functions. You'll get bonus points if your test file uses the rSpec testing framework! ;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment