Destructuring assignment in Ruby
General REPL usage:
# bundle exec irb
>> require 'destruct_repl'
=> true
>> p([1, Var.new(:x)]).match([1, 2])
=> #<Env: x=2>
>> transform(ast { [1, x] }).match([1, 2])
=> #<Env: x=2>
Show code:
cpat = compile([1, Var.new(:x)])
cpat.show_code
Show transformations:
$show_transformations = true
transform(ast { [1, v, 3 | 4] })
types.rb
- pattern matchersstandard_pattern.rb
- invented syntax for pattern matchersdestruct_spec.rb
- example case/when usage.
If you get a "No method 'eval' on NilClass" error or "binding must be provided" error,
pass binding
to any methods that take it as an optional parameter:
Point = Struct.new(:x, :y)
transform(ast { Point[x: 1, y: v] }, binding)