Skip to content

Navigation Menu

Sign up

Idea: Preserve matchdata in rules #437

avdi started this conversation in Ideas
Discussion options

Here's part of a rule I just wrote the other day:

stampfile_pattern = %r"^members/(?<email>.*)/\d{2}-(?<step>\w+).timestamp$"
rule stampfile_pattern do |t|
 match = stampfile_pattern.match(t.name)
 email = match[:email]
 step = match[:step]
 # ...
end

Notice how, after it matches, I have to re-apply the regex inside the rule definition in order to pull out named matchdata.

I was thinking of introducing something like this as an alternative:

stampfile_pattern = %r"^members/(?<email>.*)/\d{2}-(?<step>\w+).timestamp$"
rule stampfile_pattern do |t|
 email = t.matchdata[:email]
 step = t.matchdata[:step]
 # ...
end

Before I dive in, what do you think? And is there another way to accomplish this that I've missed?

You must be logged in to vote

Replies: 5 comments

Comment options

I think this would be a nice addition.

I don't think there's a way to accomplish this without adding the feature to the rule.

I've not used a rule without dependencies, it gets triggered by a separate task that depends on the output (regexp)?

You must be logged in to vote
0 replies
Comment options

I don't think there's a way to accomplish this without adding the feature to the rule.

Yeah, that's what I was thinking. I feel like it makes sense, since the task object already has the concrete t.name that matched, to also have the matchdata.

I've not used a rule without dependencies, it gets triggered by a separate task that depends on the output (regexp)?

Yep.

You must be logged in to vote
0 replies
Comment options

I've also thought about this:

rule %r"^members/(?<email>.*)/\d{2}-(?<step>\w+).timestamp$" => 
 "members/%{email}/some-input-file" do |t|
 email = t.matchdata[:email]
 step = t.matchdata[:step]
 # ...
end

I.e. adding the ability to substitute in backreferences in the rule source. Currently, rule sources suffer from the same problem as the rule body: if you want to reference regex matchdata, you have to use a lambda as the dependency and re-apply the regex.

You must be logged in to vote
0 replies
Comment options

I stumbled upon this issue looking for how to access matchdata inside a rule. This would be a great addition, imho.

You must be logged in to vote
0 replies
Comment options

Oh hey, thanks for reminding me.
...
On Fri, Dec 2, 2016 at 9:07 AM, Anurag Priyam ***@***.***> wrote: I stumbled upon this issue looking for how to access matchdata inside a rule. This would be a great addition, imho. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <#141 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAAD1vT-M-mathKpiMBZoe60ozhgaAU8ks5rECYkgaJpZM4I20fQ> .
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
Converted from issue

This discussion was converted from issue #141 on June 21, 2022 22:19.

AltStyle によって変換されたページ (->オリジナル) /