syntax/local_binding_forms.rb
tests/_examples/syntax/local_binding_forms.rb in the metorex repository.
# Each form that binds a local makes `name [1]` index that local from then
# on, where before it the same words call a method named `name` with an
# Array.
class IndexedError < StandardError
def [](index) = message[index]
end
def first(*arguments) = [:method, arguments]
def second(*arguments) = [:method, arguments]
def rest(*arguments) = [:method, arguments]
def error(*arguments) = [:method, arguments]
def item(*arguments) = [:method, arguments]
def hidden(*arguments) = [:method, arguments]
def match(*arguments) = [:method, arguments]
def held(*arguments) = [:method, arguments]
def matched(*arguments) = [:method, arguments]
p(first [1])
(first, (second, *rest)), _ = [["a", ["b", "c", "d"]], nil]
p(first [0], second [0], rest [1])
begin
raise IndexedError, "boom"
rescue => error
p(error [0])
end
for item in [[7, 8]]
p(item [1])
end
[[5, 6]].each { |pair; hidden| hidden = pair; p(hidden [0]) }
p(hidden [0])
[[3, 4]].each { p(_1 [1]) }
[[1, 2]].each do p(_1 [0]) end
if /(?<match>\w+)/ =~ "word"
p(match [0])
end
held = [9]
case [[9]]
in [^held] then p(held [0])
end
case [1, 2]
in [Integer, _] | [String, _] => matched then p(matched)
end
p(matched [0])
Output:
[:method, [[1]]]
"a"
"b"
"d"
"b"
8
5
[:method, [[0]]]
4
1
"w"
9
[1, 2]
1