Skip to content

Instantly share code, notes, and snippets.

@forresty
Last active June 28, 2018 08:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forresty/beca2930f394e847789b2c9e0351523f to your computer and use it in GitHub Desktop.
Save forresty/beca2930f394e847789b2c9e0351523f to your computer and use it in GitHub Desktop.
class WagonString
def initialize(string)
# TODO: store the string as array of characters
end
def to_s
# TODO: convert into a Ruby String
end
def reverse
# TODO: return a new WagonString with reversed characters
end
def reverse!
# TODO:
# - reverse the characters in-place
# - return itself without creating a new WagonString instance
end
end
def assert(condition, message = "Panic")
raise message unless condition
end
def test_internal_data
hello = WagonString.new('hello')
assert hello.instance_variables.size == 1, 'should contain one and only one ivar'
ivar = hello.instance_variables.first
assert hello.instance_variable_get(ivar).is_a?(Array), 'the ivar should be an array'
end
def test_to_s
hello = WagonString.new('hello')
assert hello.to_s == 'hello', "should convert into String 'hello'"
end
def test_reverse
hello = WagonString.new('hello')
assert hello.reverse.to_s == 'olleh', "should reverse into WagonString 'olleh'"
end
def test_reverse_return_wagon_string
hello = WagonString.new('hello')
assert hello.reverse.is_a?(WagonString), "should return a WagonString"
end
def test_reverse_do_not_modify_original
hello = WagonString.new('hello')
hello.reverse
assert hello.to_s == 'hello', "should not change the original object"
end
def test_reverse_bang
hello = WagonString.new('hello')
assert hello.reverse!.to_s == 'olleh', "should reverse into WagonString 'olleh'"
end
def test_reverse_bang_return_wagon_string
hello = WagonString.new('hello')
assert hello.reverse!.is_a?(WagonString), "should return a WagonString"
end
def test_reverse_new_object
hello = WagonString.new('hello')
assert hello.object_id != hello.reverse.object_id, "should return a new object"
end
def test_reverse_bang_no_new_object
hello = WagonString.new('hello')
assert hello.object_id == hello.reverse!.object_id, "should return the same object"
end
test_internal_data
test_to_s
test_reverse
test_reverse_return_wagon_string
test_reverse_do_not_modify_original
test_reverse_bang
test_reverse_bang_return_wagon_string
test_reverse_new_object
test_reverse_bang_no_new_object
puts "yay it works"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment