Skip to content

Instantly share code, notes, and snippets.

@allolex
Created October 13, 2015 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allolex/3610e013e7392a889af7 to your computer and use it in GitHub Desktop.
Save allolex/3610e013e7392a889af7 to your computer and use it in GitHub Desktop.
def add_two(number = 0, *rest)
return nil unless number.respond_to? :+
# if number.respond_to? :+
if number.respond_to? :concat
puts "srsly?" if rest.size > 0
if number.respond_to? :push
number.push 2
else
number.concat "2"
end
else
rest << number
rest_sum = 0
rest.each do |num|
rest_sum += num
end
2 + rest_sum
end
# end
end
def add_five number
# …
end
def test_add_two
p add_two(2) == 4
p add_two(1) == 3
p add_two(1.0) == 3.0
p add_two("a") == "a2"
p add_two([]) == [2]
p add_two([1]) == [1,2]
p add_two({}) == nil
p add_two(nil) == nil
p add_two(true) == nil
p add_two == 2
p add_two(1,2) == 5
p add_two("a", "b")
end
def test_add_five
# test with expectation for add_five
end
def test_all
test_add_two
test_add_five
end
test_all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment