annotate 1-Ruby/logic.rb @ 44:6347bacb3be7

Add more notes about the DSL while I think about it Note: Several things don't make sense
author IBBoard <dev@ibboard.co.uk>
date Mon, 18 Sep 2017 20:53:36 +0100
parents e5b84cc7bc29
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 #! /usr/bin/env ruby
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 x = 4
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 # "if" block is "if… end" - no "then" required
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 if x == 4
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 puts "x equals 4"
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 end
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 # Python- and Perl-like following logic also works
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 puts "x equals 4" if x == 4
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 puts "x equals 4" unless x != 4
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 # Negation has many forms.
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 # Simple logic:
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 if x != 5
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 puts "x not equal 5"
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 end
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 # Two options on negation
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 if !(x == 5)
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 puts "x not equal 5"
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 end
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 if not x == 5
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 puts "x not equal 5"
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 end
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 # "unless" blocks instead of "if"
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 unless x != 4
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 puts "x equal 4"
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 else
700c167cad9f Add some basic logic examples
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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