一般语言中,函数必须有返回值,即要带个return关键字。但在ruby中,return不是必须的,如果不写会默认返回最终计算的结果。举例
def add(a,b)
# 省去了return
a + b
end puts add 2,3
注意看下面的结果
def a
puts "A was evaluated!"
return true
end def b
puts "B was also evaluated!"
return true
end puts a || b
puts "------"
puts a && b
结果
A was evaluated!
true
------
A was evaluated!
B was also evaluated!
true
|| 当 a为真就不执行b了
&& 若a为真还会继续执行b