Away0x's Blog

Coding blogging for hackers.

Ruby - Control Flow

if

1
2
3
4
5
6
7
8
9
10
11
12
# 可以省略 then

if expr then
    code ...
elsif expr2 then
    code ...
else expr2
    code ...
end

# if 表达式 (ruby 的 if 有返回值)
puts "a > b" if a > b

unless

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# unless 和 if 相反,条件为假时执行

unless expr
    条件1
else
    条件2

# 与上等价
if expr
    条件2
else
    条件1

# unless 也有返回值,因此也可写成表达式形式
a = 2 unless defined?(b)

case

  • case 语句在判断与 when 指定的值是否相等时,实际上是用 === 来判断的
    • === 作用详见运算符笔记
1
2
3
4
5
6
7
8
9
10
# 可以省略 then

case 比较对象
when 1 then
    code...
when 2 then
    code...
else
    code...
end
  • 由于 case 内部是 === 所以不止可比较简单类型
1
2
3
4
5
6
7
8
9
10
case a
when 1
    1
when /hello/
    "hello world"
when Array
    []
else
    'ok'
end

times

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# do ~ end
7.times do
    puts "lalala"
end

# { ~ }
7.times {
    puts "lalala"
}

# 获取 index
7.times do |i|
    puts "第 #{i} 次的循环"
end

for

1
2
3
4
5
6
7
8
9
10
# do 可省略

for i in 1..5 do
    puts i
end

names = ["A", "B", "C"]
for name in names
    puts name
end

while</h2

1
2
3
4
5
6
7
# do 可省略

i = 1
while i < 3 do
    puts i
    i += 1
end

until

  • until 是不满足条件才执行循环处理,满足则退出,和 while 相反
1
2
3
4
5
6
7
8
# do 可省略

sum = 0
i = 1
until sum >= 50
    sum += i
    i += 1
end

each

  • 可将对象集合中的对象逐个取出,类似 for…in
  • ruby 内部 for 实际是用 each 实现的
1
2
3
4
5
6
# do ~ end 可换成 { ~ }

names = ["A", "B", "C"]
names.each do |name|
    puts name
end

loop

  • 没用终止的循环,不断执行
1
2
3
loop do
    print "lalala"
end

break

终止程序,跳出循环

next

跳到下一次循环

redo

在相同的条件下重复刚才的处理