Created
March 3, 2015 14:20
-
-
Save katiebuilds/7dccfc74668eb9b84ce2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'minitest/autorun' | |
require 'minitest/pride' | |
# Write a class which wraps around an array, but only allows odd numbers | |
# to be stored in the array. | |
# WRITE YOUR CODE HERE. | |
class OddArray | |
def initialize(array) | |
@array = array.select {|num| num.odd?} | |
end | |
def to_a | |
@array | |
end | |
def add(number) | |
if number.odd? | |
@array << number | |
end | |
end | |
end | |
#passes all tests, not sure about redefining to_a, but didn't know how else to do it | |
class CompositionChallenge < MiniTest::Test | |
def test_class_exists | |
assert OddArray | |
end | |
def test_initializer_takes_array_parameter | |
assert OddArray.new([1, 3, 5]) | |
end | |
def test_to_a | |
odd_array = OddArray.new([1, 3, 5]) | |
assert_equal [1, 3, 5], odd_array.to_a | |
end | |
def test_add_number | |
odd_array = OddArray.new([1, 3, 5]) | |
odd_array.add(7) | |
assert_equal [1, 3, 5, 7], odd_array.to_a | |
end | |
def test_initialize_with_evens | |
odd_array = OddArray.new([1, 2, 3, 4, 5]) | |
assert_equal [1, 3, 5], odd_array.to_a | |
end | |
def test_add_evens | |
odd_array = OddArray.new([1, 3, 5]) | |
odd_array.add(2) | |
assert_equal [1, 3, 5], odd_array.to_a | |
end | |
def test_add_negatives | |
odd_array = OddArray.new([-1, -2, 3, 4, -5]) | |
odd_array.add(-4) | |
assert_equal [-1, 3, -5], odd_array.to_a | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment