ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "baweaver (Brandon Weaver) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "baweaver (Brandon Weaver)" <noreply@ruby-lang.org>
Subject: [ruby-core:117233] [Ruby master Feature#20349] Pattern Matching - Expose local variable captures
Date: Tue, 19 Mar 2024 17:58:11 +0000 (UTC)	[thread overview]
Message-ID: <redmine.issue-20349.20240319175811.7576@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-20349.20240319175811.7576@ruby-lang.org

Issue #20349 has been reported by baweaver (Brandon Weaver).

----------------------------------------
Feature #20349: Pattern Matching - Expose local variable captures
https://bugs.ruby-lang.org/issues/20349

* Author: baweaver (Brandon Weaver)
* Status: Open
----------------------------------------
In Regular Expressions we have the ability to utilize `Regexp.last_match` ([link](https://ruby-doc.org/3.2.2/Regexp.html#method-c-last_match)) 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:

```ruby
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
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

       reply	other threads:[~2024-03-19 17:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-19 17:58 baweaver (Brandon Weaver) via ruby-core [this message]
2024-03-19 20:01 ` [ruby-core:117235] [Ruby master Feature#20349] Pattern Matching - Expose local variable captures Eregon (Benoit Daloze) via ruby-core
2024-03-20  8:32 ` [ruby-core:117248] " nobu (Nobuyoshi Nakada) via ruby-core
2024-03-20 11:05 ` [ruby-core:117256] " Eregon (Benoit Daloze) via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.issue-20349.20240319175811.7576@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=noreply@ruby-lang.org \
    --cc=ruby-core@ml.ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).