Skip to content

Commit

Permalink
add Object#while:do:else: & Block#while_true:else:
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkdoor committed Dec 17, 2012
1 parent 4894d71 commit 8fc2be1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/block.fy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ class Block {


alias_method: 'while_do: for: 'while_true: 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 { def until_do: block {
""" """
@block @Block@ to be called while @self yields @nil or @false. @block @Block@ to be called while @self yields @nil or @false.
Expand Down
12 changes: 11 additions & 1 deletion lib/object.fy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -240,7 +240,17 @@ class Object {
cond_block while_do: body_block 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 { def until: cond_block do: body_block {
Expand Down
18 changes: 18 additions & 0 deletions tests/block.fy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ FancySpec describe: Block with: {
i is be: { i >= 10 } 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: { it: "calls a block while another is not true (boolean false)" with: 'while_false: when: {
i = 0 i = 0
{i == 10} while_false: { {i == 10} while_false: {
Expand Down

0 comments on commit 8fc2be1

Please sign in to comment.