Skip to content

wintonpc/destruct

Repository files navigation

destruct

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 matchers
  • standard_pattern.rb - invented syntax for pattern matchers
  • destruct_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)

About

Destructuring assignment in Ruby

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published