# Rule with regex executed twice for same matching string

**URL:** <https://www.ruby-forum.com/t/rule-with-regex-executed-twice-for-same-matching-string/75156>\
**Category:** Ruby\
**Created:** [November 7, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/rule-with-regex-executed-twice-for-same-matching-string/75156 "2006-11-07T20:29:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [November 7, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/rule-with-regex-executed-twice-for-same-matching-string/75156/1 "2006-11-07T20:29:49Z")

</div>

Does anyone know why this rule is executed twice?

$ cat rakefile  
task :default =\> “foo”

deps = [proc {“bar”}, proc {“barney”}]  
rule /^foo$/ =\> deps

rule /^bar$/ do |t|  
puts t.name  
end

$ rake --trace  
(in /home/vjoel/tmp/raketest)  
\*\* Invoke default (first\_time)  
\*\* Invoke foo (first\_time)  
\*\* Invoke bar (first\_time)  
\*\* Execute bar  
bar  
bar  
\*\* Execute foo  
\*

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [November 10, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/rule-with-regex-executed-twice-for-same-matching-string/75156/2 "2006-11-10T21:33:29Z")

</div>

This seems to be a bug in rake. It affects rules with multiple  
prerequisites (but only if at least one prerequisite after the first is  
a rule).

Here’s a patch against the current rake gem version, 0.7.1:

$ diff -u rake.rb.bck rake.rb  
— rake.rb.bck 2006-11-10 12:11:32.000000000 -0800  
+++ rake.rb 2006-11-10 12:11:32.000000000 -0800  
@@ -1566,7 +1566,7 @@  
prereqs = sources.collect { |source|  
if File.exist?(source) || Rake::Task.task\_defined?(source)  
source

- 

```
  elsif parent = enhance_with_matching_rule(sources.first, 

```

level+1)

- 

```
  elsif parent = enhance_with_matching_rule(source, level+1)
     parent.name
   else
     return nil
```
