IF 语句和unless语句
本质上,unless 与 if 的意思,刚好相反
#if条件语句
x = 10
y = 20
if x == y
puts "x is the same as y"
else
puts "x and y are not the same"
end
#if嵌套
if x == y
puts "x is equal to y"
elsif x >= z
puts "x is greater than or equal to z"
elsif x < z
puts "x is less than y"
else
puts "Something else"
end
#unless 条件语句(除非...)
players = ["Correa", "Carter", "Altuve"]
unless players.empty?
players.each { |player| puts player}
end
#同上
players.each{ |player| puts player } unless players.empty?
players.each{ |player| puts player } if !players.empty?
players2 =[]
unless players2.empty?
players2.each { |player| puts player}
end
The following is the list of arithmetic functions to use in a Ruby program:
+: 加法
-: 减法
/: 除法
*: 乘法
**: 乘方
你可以在这里看到一步一步的看到他的执行过程
5 + 15 * 20 - 2 / 6 ** 3 - (3 + 1) # parenthesis
5 + 15 * 20 - 2 / 6**3 - 4 # exponents
5 + 15 * 20 - 2 / 216 - 4 # multiplication
5 + 300 - 2 / 216 - 4 # division
5 + 300 - 0 - 4 # addition
305 - 0 - 4 # subtraction
301
优先级的记忆PEMDAS
Please Excuse My Dear Aunt Sally
(请原谅我亲爱的萨莉阿姨),即:
- P: 括号
- E: 乘方
- M: 乘法
- D: 除法
- A: 加法
- S: 减法
关系运算符
>: Greater than
<: Less than
>=: Greater than or equal to
<=: Less than or equal to
!=: Not equal to