Project

General

Profile

Actions

Feature #20349

open

Pattern Matching - Expose local variable captures

Added by baweaver (Brandon Weaver) about 2 months ago. Updated about 2 months ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:117233]

Description

In Regular Expressions we have the ability to utilize Regexp.last_match (link) to access the most recent match data from a regular expression match. I would like to propose that we have similar functionality for pattern matching:

require "rubocop"

def ast_from(string)
  case string
  when String then processed_source_from(string).ast
  when RuboCop::ProcessedSource then string.ast
  when RuboCop::AST::Node then string
  else nil
  end
end

def longform_block?(node)
  node in [:block,                        # s(:block,
    [:send, target, target_method],    #   s(:send, s(:array), :select),
    [[:arg, v]],                       #   s(:args, s(:arg, :v)),
    [:send, [:lvar, ^v], block_method] #   s(:send, s(:lvar, :v), :even?))
  ]
end

longform_block?(ast_from("[1, 2, 3].select { |v| v.even? }"))
# => true

# Name is not important, idea is, name can be debated. `source` is used to not
# show AST nodes
PatternMatch.last_match.transform_values(&:source)
# => {:node=>"[1, 2, 3].select { |v| v.even? }",
# :result=>"[1, 2, 3].select { |v| v.even? }",
# :target=>"[1, 2, 3]",
# :target_method=>:select,
# :v=>:v,
# :block_method=>:even?}

# Hacky version to show how / where behavior could be captured
def longform_block?(node)
  result = node in [:block,                        # s(:block,
    [:send, target, target_method],    #   s(:send, s(:array), :select),
    [[:arg, v]],                       #   s(:args, s(:arg, :v)),
    [:send, [:lvar, ^v], block_method] #   s(:send, s(:lvar, :v), :even?))
  ]

  pp(binding.local_variables.to_h { [_1, binding.local_variable_get(_1).then { |s| s.source rescue s }] })

  result
end
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0