Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 1-Ruby/blocks.rb @ 16:8d46064c9afc
Be a nice Ruby user and follow block convention for do/end vs {}
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 03 Jan 2017 20:38:45 +0000 |
parents | 0374ae06729e |
children |
rev | line source |
---|---|
10
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 #! /usr/bin/env ruby |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 x = 1 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 # Inline loops |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 x = x + 1 while x < 10 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 puts x |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 x = x - 1 until x == 1 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 puts x |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 # Conventional loops |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 while x < 10 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 x = x + 1 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 end |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 puts x |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 until x == 1 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 x = x - 1 |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 end |
0374ae06729e
Start playing around with loop block definitions
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 puts x |