2010-01-29 23:30:54 -05:00
|
|
|
puts(if my_special_variable? then false else true)
|
2010-01-01 12:41:55 -05:00
|
|
|
|
|
|
|
my_special_variable: false
|
|
|
|
|
2010-01-29 23:30:54 -05:00
|
|
|
puts(if my_special_variable? then true else false)
|
2010-01-16 23:03:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Existential assignment.
|
|
|
|
|
|
|
|
a: 5
|
|
|
|
a: null
|
|
|
|
a ?= 10
|
|
|
|
b ?= 10
|
|
|
|
|
2010-01-29 23:30:54 -05:00
|
|
|
puts a is 10 and b is 10
|
2010-01-16 23:03:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
# The existential operator.
|
|
|
|
|
|
|
|
z: null
|
|
|
|
x: z ? "EX"
|
|
|
|
|
2010-01-29 23:30:54 -05:00
|
|
|
puts z is null and x is "EX"
|
2010-01-17 14:23:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Only evaluate once.
|
|
|
|
|
|
|
|
counter: 0
|
2010-01-26 10:52:05 -05:00
|
|
|
get_next_node: ->
|
2010-01-17 14:23:41 -05:00
|
|
|
throw "up" if counter
|
|
|
|
counter++
|
|
|
|
|
2010-01-29 23:30:54 -05:00
|
|
|
puts(if get_next_node()? then true else false)
|
2010-01-23 23:30:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Existence chains, soaking up undefined properties:
|
|
|
|
|
|
|
|
obj: {
|
|
|
|
prop: "hello"
|
|
|
|
}
|
|
|
|
|
2010-01-29 23:30:54 -05:00
|
|
|
puts obj?.prop is "hello"
|
2010-01-23 23:30:55 -05:00
|
|
|
|
2010-02-02 10:43:23 -05:00
|
|
|
puts obj.prop?.length is 5
|
2010-02-02 10:39:44 -05:00
|
|
|
|
2010-02-02 10:43:23 -05:00
|
|
|
puts obj?.prop?.non?.existent?.property is undefined
|
2010-01-24 12:52:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Soaks and caches method calls as well.
|
|
|
|
|
|
|
|
arr: ["--", "----"]
|
|
|
|
|
2010-01-29 23:30:54 -05:00
|
|
|
puts arr.pop()?.length is 4
|
|
|
|
puts arr.pop()?.length is 2
|
|
|
|
puts arr.pop()?.length is undefined
|
|
|
|
puts arr[0]?.length is undefined
|
|
|
|
puts arr.pop()?.length?.non?.existent()?.property is undefined
|