Skip to content
Browse files

add Object#while:do:else: & Block#while_true:else:

  • Loading branch information...
1 parent 4894d71 commit 8fc2be11438fda8eb0e18b26087c6c0cfbc66701 @bakkdoor committed Dec 17, 2012
Showing with 41 additions and 1 deletion.
  1. +12 −0 lib/block.fy
  2. +11 −1 lib/object.fy
  3. +18 −0 tests/block.fy
View
12 lib/block.fy
@@ -51,6 +51,18 @@ class Block {
alias_method: 'while_do: for: 'while_true:
+ def while_true: block else: alternative {
+ """
+ @block @Block@ to be called while @self yields @true.
+ @alternative @Block@ to be called if @self never yielded @true.
+ """
+
+ if: call then: {
+ block call
+ while_true: block
+ } else: alternative
+ }
+
def until_do: block {
"""
@block @Block@ to be called while @self yields @nil or @false.
View
12 lib/object.fy
@@ -240,7 +240,17 @@ class Object {
cond_block while_do: body_block
"""
- cond_block while_do: body_block
+ cond_block while_true: body_block
+ }
+
+ def while: condition do: body else: alternative {
+ """
+ @condition @Block@ to be used as condition for while loop.
+ @body @Block@ to be called while @condition yields @true.
+ @alternative @Block@ to be called if @body never got called (@condition never yielded @true).
+ """
+
+ condition while_true: body else: alternative
}
def until: cond_block do: body_block {
View
18 tests/block.fy
@@ -32,6 +32,24 @@ FancySpec describe: Block with: {
i is be: { i >= 10 }
}
+ it: "calls a block while another is true or calls the alternative" with: 'while_true:else: when: {
+ i = 0
+ { i < 10 } while_true: {
+ i = i + 1
+ } else: {
+ i = "nope"
+ }
+ i is: 10
+
+ i = 0
+ { i > 10 } while_true: {
+ i = i + 1
+ } else: {
+ i = "nope"
+ }
+ i is: "nope"
+ }
+
it: "calls a block while another is not true (boolean false)" with: 'while_false: when: {
i = 0
{i == 10} while_false: {

0 comments on commit 8fc2be1

Please sign in to comment.
Something went wrong with that request. Please try again.