Skip to content

Instantly share code, notes, and snippets.

@allolex
Created October 13, 2015 20:10
Show Gist options
  • Save allolex/3610e013e7392a889af7 to your computer and use it in GitHub Desktop.
Save allolex/3610e013e7392a889af7 to your computer and use it in GitHub Desktop.

Revisions

  1. allolex created this gist Oct 13, 2015.
    51 changes: 51 additions & 0 deletions tdd_methods.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    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