赋值
local = 1 # 变量赋值
@instance = 2 # 实例属性赋值
@@class = 3 # 静态类属性赋值
#同时支持的一些操作符
local += 1
# +, -, *, /, %, |, &, ^, **, <<, >> #这些也被上面的表达式支持
local ||= 1 # same as: local || (local = 1)
local &&= 1 # same as: local && (local = 1)
#类和对象的支持
person.name = "John"
objects.[]=(2, 3) # same as : objects[2] = 3
objects.[](2, 3) # same as: objects[2, 3]
person.name ||= "John" # same as: person.name || (person.name = "John")
person.name &&= "John" # same as: person.name && (person.name = "John")
objects[1] += 2 # same as: objects[1] = objects[1] + 2
objects[1] ||= 2 # same as: objects[1]? || (objects[1] = 2)
objects[1] &&= 2 # same as: objects[1]? && (objects[1] = 2)
多重赋值
name, age = "Crystal", 1
a, b = b, a
name, age, source = "Crystal,1,github".split(",")
names = "John", "Peter", "Jack" # same as : names = ["John", "Peter", "Jack"]
# 类和对象的支持
objects[1], objects[2] = 3, 4
当前内容版权归 crystal-lang中文站 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 crystal-lang中文站 .