Project

General

Profile

Bug #10123

Named capture groups don't set local vars when matched using ~

Added by Josh Cheek over 1 year ago. Updated 6 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
ruby -v:
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
[ruby-core:64304]

Description

Broken when using ~:

$ echo ' josh' | ruby -ne '~/(?<initial>\w)/ and puts initial'
-e:1:in `<main>': undefined local variable or method `initial' for main:Object (NameError)

Working when using =~ $_:

$ echo ' josh' | ruby -ne '/(?<initial>\w)/ =~ $_ and puts initial'
j

History

#1 [ruby-core:64603] Updated by Nobuyoshi Nakada over 1 year ago

  • Description updated (diff)

I'm not sure if this should be a bug or a new feature.

#2 [ruby-core:71320] Updated by Josh Cheek 6 months ago

Josh Cheek wrote:

Broken when using ~:

$ echo ' josh' | ruby -ne '~/(?<initial>\w)/ and puts initial'
-e:1:in `<main>': undefined local variable or method `initial' for main:Object (NameError)

Working when using =~ $_:

$ echo ' josh' | ruby -ne '/(?<initial>\w)/ =~ $_ and puts initial'
j

Similarly, these do not set the local variable.

$ echo ' josh' | ruby -ne '/(?<initial>\w)/ !~ $_ or puts initial'
$ echo ' josh' | ruby -ne '$_[/(?<initial>\w)/] and puts initial'
$ echo ' josh' | ruby -ne 'puts initial if /(?<initial>\w)/'
$ echo ' josh' | ruby -ne 'if /(?<initial>\w)/ then puts initial end'

Also available in: Atom PDF