Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 1-Ruby/logic.rb @ 103:98be775c533c default tip
An odd "non-determinism" example from StackOverflow
It is clever, but doesn't make much sense as to how it gets its results
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 14 Jul 2019 13:44:13 +0100 |
parents | e5b84cc7bc29 |
children |
rev | line source |
---|---|
8 | 1 #! /usr/bin/env ruby |
2 | |
3 x = 4 | |
4 | |
5 # "if" block is "if… end" - no "then" required | |
6 if x == 4 | |
7 puts "x equals 4" | |
8 end | |
9 | |
10 # Python- and Perl-like following logic also works | |
11 puts "x equals 4" if x == 4 | |
12 puts "x equals 4" unless x != 4 | |
13 | |
14 # Negation has many forms. | |
15 # Simple logic: | |
16 if x != 5 | |
17 puts "x not equal 5" | |
18 end | |
19 | |
20 # Two options on negation | |
21 if !(x == 5) | |
22 puts "x not equal 5" | |
23 end | |
24 | |
25 if not x == 5 | |
26 puts "x not equal 5" | |
27 end | |
28 | |
29 # "unless" blocks instead of "if" | |
30 unless x != 4 | |
31 puts "x equal 4" | |
32 else | |
33 puts "x not equals 4" | |
12
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
34 end |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
35 |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
36 # "then" is optional, except in single line statement |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
37 if x == 4 then puts "x equal 4" end |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
38 if x == 4 then |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
39 puts "x equal 4" |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
40 end |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
41 if x == 4 |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
42 puts "x equal 4" |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
43 end |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
44 # The following line gives: |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
45 # logic.rb:…: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n' |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
46 # if x == 4 puts "x equal 4" end |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
47 # |
e5b84cc7bc29
Update logic script re: "then" in "if else" statements
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
48 # if x == 4 puts "x equal 4" end |