Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 86 lines (72 sloc) 1.671 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
class Fancy {
  class BreakIteration : StdError {
    """
Raised to break the current iteration.
It is rescued by Block#loop.

Example:
10 times: |i| {
i println
if: (i == 3) then: {
Fancy BreakIteration new raise!
}
}
\"Done!\" println

Produces:
0
1
2
3
Done!
"""

    read_slots: ['result]
    def initialize: @result
  }

  class NextIteration : StdError {
    """
Raised to continue with next iteration (and stopping the current one).
It is rescued by Block#loop.
"""

    read_slots: ['result]
    def initialize: @result
  }

  class StopIteration : StdError {
    """
Raised to stop the iteration, in particular by Enumerator#next.
It is rescued by Block#loop.

Example:
{
'Hello println
Fancy StopIteration new raise!
'World println
} loop
'Done! println

Produces:
Hello
Done!
"""

    def initialize { @result = nil }
    def initialize: @result

    def result {
      """
Returns the return value of the iterator.

o = Object new
def o each: block {
block call: [1]
block call: [2]
block call: [3]
100
}

e = o to_enum
e next p #=> 1
e next p #=> 2
e next p #=> 3
try {
e next
} catch Fancy StopIteration => ex {
ex result p #=> 100
}
"""

      @result
    }
  }
}
Something went wrong with that request. Please try again.