Created
June 29, 2012 14:41
-
-
Save ralphos/3018324 to your computer and use it in GitHub Desktop.
UniqueArray Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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