-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path2-expressions_and_operators.rb
60 lines (50 loc) · 1.08 KB
/
2-expressions_and_operators.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# ====================================
# expressions and operators
# ====================================
# ruby's syntax is expression-oriented.
# control structures such as 'if' that
# would be called statements in other
# languages are actually expressions in
# ruby.
# =================
# quick examples of
# operators
# =================
1 + 2 # => 3: addition
2 * 3 # => 6: multiplication
1 + 2 == 3 # => true: == tests equality
2 ** 1024 # => 2 to the power of 1024
"dog" + "pound" # => "dogpound"
"hi " * 3 # => "hi hi hi "
# =================
# if
# =================
if 2 + 2 == 5
#...
else
#...
end
# =================
# unless
# =================
unless "5".respond_to?(:each)
#...
else
#...
end
# more commonly used like this...
#
puts "welcome!" unless ["Jill", "Bob", "Allen"][rand(3)] == "Bob"
# or...
# puts report.path unless report.path.nil?
# =================
# case
# =================
case rand(20)
when 0..5
#...
when 6..10
#...
else
#...
end