Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Assigning regex with let causes spec to fail - unless the variable is invoked prior to use. #820

@forforf

Description

@forforf

Stackoverflow question with code to reproduce the issue:

In a nutshell, if I assign a regex with a capture using a let statement. Something like this:

 let(:my_matcher) { /RegexWithCapture(\d)/ }

Then it fails in the it block when I actually try to use my_matcher

 "RegexWithCapture7".match(my_matcher)[1].should == 7  
 #=> fails
 #=> NoMethodError:  undefined method `[]' for /RegexWithCapture(\d)$/:Regexp

But!, If I invoke the matcher prior to using it, everything works as I'd expect.

my_matcher  #just invoking it does the trick
"RegexWithCapture7".match(my_matcher)[1].should == 7  
 #=> passes

I haven't tested the above code, I'm just using it to explain the problem but the stackoverflow question has an actual reproducible case. Here's a Gist with the same code as the SO question: https://gist.github.com/forforf/5112445 it's a bit easier to read.

I'm really curious why the behavior changes by just invoking the let variable.

Ruby 1.9.3 & RSpec 2.13.0 fwiw.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions