1. 基本表达式
没啥好说的~~~~~ 唯一要注意的就是 Ruby 不支持自加(++)和自减(--),可以使用 += 和 -= 代替。
1 + 2 a = 3 a += 4
puts `ver`
def `(cmd) "Command:#{cmd}\n" + Kernel.`(cmd) end puts `ver`
a = b = c = 1 # a = 1, b = 1, c = 1 a = (b = 1 + 2) + 3 # a = 6, b = 3
a = 1 b = 2 a, b = b, a puts a, b
x = 0 a, b, c = x, (x += 1), (x += 2) puts a, b, c
a, b, c = 0, 1 puts a, b, c
a = 0, 1 puts a.class puts a
a, b, *c = 1, 2, 3, 4, 5 puts a puts b puts "------------" puts c
a = [1, 2, 3, 4, 5] b, c = 99, a # b=99, c=[1,2,3,4,5] b, *c = 99, a # b=99, c=[[1,2,3,4,5]] b, c = 99, *a # b=99, c=1 b, *c = 99, *a # b=99, c=[1,2,3,4,5]
a, (b, c), d = 1, 2, 3, 4 puts a, b, c, d
a, (b, c), d = 1, [2, 3], 4 puts a, b, c, d
a, (b, *c), d = 1, [2, 3, 100], 4 puts b, "----", c
while line = gets puts line end
puts defined?(a) a = 1 puts defined?(a)
puts "a" && "b" # a 为真,所以决定表达式真伪的是 b,返回 b。 puts nil && "b" # nil 导致短路求解,故返回 nil。 puts "a" || "b" # 同样是短路求解,返回 a。