Skip to content

Instantly share code, notes, and snippets.

@tamouse
Forked from jaw6/blocks.rb
Last active January 3, 2016 05:09
Show Gist options
  • Select an option

  • Save tamouse/8413785 to your computer and use it in GitHub Desktop.

Select an option

Save tamouse/8413785 to your computer and use it in GitHub Desktop.

Revisions

  1. tamouse revised this gist Jan 14, 2014. 2 changed files with 8 additions and 22 deletions.
    4 changes: 3 additions & 1 deletion blocks.rb
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,16 @@

    class List
    include Enumerable

    def initialize()
    @notes = []
    yield @notes if block_given?
    end

    attr_reader :notes

    def each
    def each &block
    @notes.each &block
    end
    end

    26 changes: 5 additions & 21 deletions result.txt
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,9 @@
    ➜ ruby blocks.rb
    Run options: --seed 47298

    Run options: --seed 58795
    # Running:

    # Running tests:
    ..

    FF
    Finished in 0.001252s, 1597.4441 runs/s, 3194.8882 assertions/s.

    Finished tests in 0.023059s, 86.7340 tests/s, 86.7340 assertions/s.

    1) Failure:
    test_detect(SimpleTestCase) [gist.rb:49]:
    --- expected
    +++ actual
    @@ -1 +1 @@
    -"From: Derek To: Josh Note: I'm not surprised"
    +""


    2) Failure:
    test_select(SimpleTestCase) [gist.rb:42]:
    Expected: 2
    Actual: 0

    2 tests, 2 assertions, 2 failures, 0 errors, 0 skips
    2 runs, 4 assertions, 0 failures, 0 errors, 0 skips
  2. @jaw6 jaw6 created this gist Jan 14, 2014.
    51 changes: 51 additions & 0 deletions blocks.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    require 'minitest/autorun'

    class List
    include Enumerable
    def initialize()
    @notes = []
    yield @notes if block_given?
    end

    attr_reader :notes

    def each
    end
    end

    class Note
    attr_accessor :from, :to, :note
    def initialize(attributes={})
    self.from = attributes[:from]
    self.to = attributes[:to]
    self.note = attributes[:note]
    end

    def to_s
    "From: #{from} To: #{to} Note: #{note}"
    end
    end


    class SimpleTestCase < MiniTest::Unit::TestCase
    def setup
    @list = List.new do |notes|
    notes << Note.new(from: "Josh", to: "Derek", note: "I'm going to be late tonight")
    notes << Note.new(from: "Derek", to: "Josh", note: "I'm not surprised")
    notes << Note.new(from: "Mo", to: "Derek", note: "Don't worry, I'll be there")
    notes << Note.new(from: "Derek", to: "Mo", note: "Thanks, Mo!")
    end
    end

    def test_select
    selected = @list.select { |note| note.from == "Derek" }
    assert_equal 2, selected.size
    assert_equal "From: Derek To: Josh Note: I'm not surprised", selected.first.to_s
    assert_equal "From: Derek To: Mo Note: Thanks, Mo!", selected.last.to_s
    end

    def test_detect
    detected = @list.detect { |note| note.to == "Josh" }
    assert_equal "From: Derek To: Josh Note: I'm not surprised", detected.to_s
    end
    end
    25 changes: 25 additions & 0 deletions result.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    ➜ ruby blocks.rb

    Run options: --seed 58795

    # Running tests:

    FF

    Finished tests in 0.023059s, 86.7340 tests/s, 86.7340 assertions/s.

    1) Failure:
    test_detect(SimpleTestCase) [gist.rb:49]:
    --- expected
    +++ actual
    @@ -1 +1 @@
    -"From: Derek To: Josh Note: I'm not surprised"
    +""


    2) Failure:
    test_select(SimpleTestCase) [gist.rb:42]:
    Expected: 2
    Actual: 0

    2 tests, 2 assertions, 2 failures, 0 errors, 0 skips