ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nobu@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:91089] [Ruby trunk Bug#8059] Unrelated block value returned by String#match(regex) when regex is returned by a method that uses define_method and super
Date: Mon, 14 Jan 2019 23:15:24 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-76320.20190114231521.e9bcf2a9174c01f5@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-8059.20130310083928@ruby-lang.org

Issue #8059 has been updated by nobu (Nobuyoshi Nakada).

Status changed from Open to Closed

Thank you for the confirmation.

----------------------------------------
Bug #8059: Unrelated block value returned by String#match(regex) when regex is returned by a method that uses define_method and super
https://bugs.ruby-lang.org/issues/8059#change-76320

* Author: myronmarston (Myron Marston)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.0]
* Backport: 
----------------------------------------
This started as a stackoverflow question:

http://stackoverflow.com/questions/15283425/in-rspec-using-let-to-assign-a-regex-creates-unexpected-pass-fail-behavior-bu/15284031

...then became an rspec-core issue:

https://github.com/rspec/rspec-core/issues/820

...which led me to isolate the behavior into a standalone script, which I've posted as a gist with an ongoing discussion with @peterc and others:

https://gist.github.com/myronmarston/5123087

Here's a simplified version of the script from the gist (removing the ENV conditionals for clarity):

class Superclass
  define_method :regex do
    /^(\d)$/
  end
end

class Subclass < Superclass
  def self.override(name)
    define_method(name) { super() }
  end

  override(:regex) { :foo }
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 get the value passed to the override block:"
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


And the output:

➜ ruby --version
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.0]
➜ ruby foo.rb 
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 get the value passed to the override block:
:foo

Unless I add a tap block that does nothing:
#<MatchData "8" 1:"8">

I can't even wrap my head around this behavior, it's so bizarre.  It appears to be fixed in 2.0.

It's also a heisenbug -- the act of trying to inspect things using the `tap` block or `set_trace_func` causes the bug to go away.

I'd love to see this fixed in a future 1.9.3 patch release.  More than that, I'm extremely curious what's causing this; it seems to violate my mental model of how ruby works so thoroughly!



-- 
https://bugs.ruby-lang.org/

      parent reply	other threads:[~2019-01-14 23:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-09 23:39 [ruby-core:53271] [ruby-trunk - Bug #8059][Open] Unrelated block value returned by String#match(regex) when regex is returned by a method that uses define_method and super myronmarston (Myron Marston)
2019-01-14 17:26 ` [ruby-core:91084] [Ruby trunk Bug#8059] Unrelated block value returned byString#match(regex) when regex is returned by a method that uses define_methodand super justin
2019-01-14 23:15 ` nobu [this message]

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.journal-76320.20190114231521.e9bcf2a9174c01f5@ruby-lang.org \
    --to=ruby-core@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).