https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112019-01-14T17:26:40ZRuby Issue Tracking SystemRuby master - Bug #8059: Unrelated block value returned by String#match(regex) when regex is returned by a method that uses define_method and superhttps://bugs.ruby-lang.org/issues/8059?journal_id=763122019-01-14T17:26:40Zjsc (Justin Collins)
<ul></ul><p>The example appears to have been a little mangled on Redmine.</p>
<p>This code reproduces the issue on 1.9.3-p327 and 1.9.3-p551:</p>
<pre><code>class Superclass
define_method :regex do
/^(\d)$/
end
end
class Subclass < Superclass
def self.override(name)
define_method(name) { super() }
end
override(:regex) { }
end
puts "Subclass.new.regex returns a regular expression object:"
puts Subclass.new.regex.inspect
puts
puts "String#match(regex) returns a MatchData object:"
puts "8".match(/^(\d)$/).inspect
puts
puts "But somehow, when I combine these, I can get nil:"
puts "8".match(Subclass.new.regex).inspect
puts
puts "Unless I add a tap block that does nothing:"
puts "8".match(Subclass.new.regex.tap { |s| }).inspect
</code></pre>
<p>The output is:</p>
<pre><code>Subclass.new.regex returns a regular expression object:
/^(\d)$/
String#match(regex) returns a MatchData object:
#<MatchData "8" 1:"8">
But somehow, when I combine these, I can get nil:
nil
Unless I add a tap block that does nothing:
#<MatchData "8" 1:"8">
</code></pre>
<p>As noted, the bug was already fixed in 2.0+. As the possibility of another 1.9.3 is extremely unlikely, I guess this issue can be closed.</p> Ruby master - Bug #8059: Unrelated block value returned by String#match(regex) when regex is returned by a method that uses define_method and superhttps://bugs.ruby-lang.org/issues/8059?journal_id=763202019-01-14T23:15:21Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Thank you for the confirmation.</p>