赋值

  1. local = 1 # 变量赋值
  2. @instance = 2 # 实例属性赋值
  3. @@class = 3 # 静态类属性赋值
  4. #同时支持的一些操作符
  5. local += 1
  6. # +, -, *, /, %, |, &, ^, **, <<, >> #这些也被上面的表达式支持
  7. local ||= 1 # same as: local || (local = 1)
  8. local &&= 1 # same as: local && (local = 1)
  9. #类和对象的支持
  10. person.name = "John"
  11. objects.[]=(2, 3) # same as : objects[2] = 3
  12. objects.[](2, 3) # same as: objects[2, 3]
  13. person.name ||= "John" # same as: person.name || (person.name = "John")
  14. person.name &&= "John" # same as: person.name && (person.name = "John")
  15. objects[1] += 2 # same as: objects[1] = objects[1] + 2
  16. objects[1] ||= 2 # same as: objects[1]? || (objects[1] = 2)
  17. objects[1] &&= 2 # same as: objects[1]? && (objects[1] = 2)

多重赋值

  1. name, age = "Crystal", 1
  2. a, b = b, a
  3. name, age, source = "Crystal,1,github".split(",")
  4. names = "John", "Peter", "Jack" # same as : names = ["John", "Peter", "Jack"]
  5. # 类和对象的支持
  6. objects[1], objects[2] = 3, 4