Ruby 条件控制与关系型数据库入门
1. Ruby 条件控制语句
在 Ruby 编程中,条件控制语句是非常重要的一部分,它可以让程序根据不同的条件执行不同的代码块。
1.1 if、else、unless 和 elsif 语句
- if 语句 :是计算机编程的核心,几乎所有代码都可以重写为 if 语句。基本语法如下:
if expression
thingsToDo
end
示例代码:
class TestbedController < ApplicationController
def index
@result = 'First is greater than or equal to last.'
first = 20
last = 25
if first < last
@result = 'First is smaller than last.'
end
end
end
在这个例子中,由于 first 的值小于 last 的值, first < last 表达式的计算结果为 true ,因此 @result 会被设置为 'First is smalle
超级会员免费看
订阅专栏 解锁全文

5万+

被折叠的 条评论
为什么被折叠?



