-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03.rb
More file actions
68 lines (58 loc) · 801 Bytes
/
03.rb
File metadata and controls
68 lines (58 loc) · 801 Bytes
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
60
61
62
63
64
65
66
67
68
#錯誤處理 throw exception
def bmi(w, h)
w / (h * h)
rescue => exception
"enter error number"
end
p bmi(1, 0)
#迴圈的處理loop
10.times do
puts "hello world"
end
#do..end or {} == Block
#same
p 1.upto(5) { |i| puts i }
#same
1.upto(5) do |i|
puts i
end
p 5.downto(1) { |i| puts i }
5.downto(1) do |i|
puts i
end
#迭代的處理lteration
array = [1, 2, 3, 4, 5]
for i in array
if (i == 3)
puts "get it #{i}"
break
end
end
x = 0
while x < 10
puts x
x += 1
end
y = 0
#until = while not
until y >= 10
puts y
y += 1
end
i = 0
loop do
puts i
i += 1
break if i > 10
end
names = ["a", "b", "c"]
#each
names.each { |name| puts name }
#each
names.each do |name|
puts name
end
#each with index
names.each.with_index do |name, x|
puts "#{x}, #{name}"
end