/(?<foo>...)/ =~ str assigns foo

e$B0J2<$N$h$&$Ke(B named capture e$B$N7k2L$r<+F0E*$KJQ?t$KBeF~$5$;$?e(B
e$B$$$N$G$9$,$I$&$G$7$g$&$+!#e(B

% ./ruby -e ’
/(?\w+)/ =~ " foo "
p word

“foo”

e$BBeF~$,5/$3$k>r7o$O!"e(Bnamed capture e$B$r4^$_!“e(B#{}
e$B$J$I$K$h$kF0E*e(B
e$B$JE83+$N4^$^[email protected],I=8=$,e(B =~ e$B$N:8JU$K%j%F%i%k$H$7$FI=e(B
e$B$l$?$H$-!”$G$9!#e(B

e$B$J$*!"%^%C%A$7$J$+$C$?>l9g$Oe(B nil
e$B$rBeF~$9$k$h$&$K$7$F$$$^$9!#e(B

Index: parse.y

— parse.y (revision 14215)
+++ parse.y (working copy)
@@ -442,6 +442,8 @@ static void reg_fragment_setenc_gen(stru
#define reg_fragment_setenc(str,options)
reg_fragment_setenc_gen(parser, str, options)
static void reg_fragment_check_gen(struct parser_params*, VALUE, int);
#define reg_fragment_check(str,options) reg_fragment_check_gen(parser,
str, options)
+static NODE reg_named_capture_assign_gen(struct parser_params parser,
VALUE regexp, NODE match);
+#define reg_named_capture_assign(regexp,match)
reg_named_capture_assign_gen(parser,regexp,match)
#else
#define remove_begin(node) (node)
#endif /
!RIPPER /
@@ -2127,6 +2129,9 @@ arg : lhs ‘=’ arg
{
/
%%%*/
$$ = match_op($1, $3);

  •                    if (nd_type($1) == NODE_LIT && TYPE($1->nd_lit) 
    

== T_REGEXP) {

  •                        $$ = reg_named_capture_assign($1->nd_lit, 
    

$$);

  •                    }
       /*%
     $$ = dispatch3(binary, $1, ripper_intern("=~"), $3);
       %*/
    

@@ -8450,6 +8455,73 @@ reg_fragment_check_gen(struct parser_par
}
}

+typedef struct {

  • struct parser_params* parser;
  • rb_encoding *enc;
  • NODE *succ_block;
  • NODE *fail_block;
  • int num;
    +} reg_named_capture_assign_t;

+static int
+reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar
*name_end,

  •      int back_num, int *back_refs, OnigRegex regex, void *arg0)
    

+{

  • reg_named_capture_assign_t arg =
    (reg_named_capture_assign_t
    )arg0;
  • struct parser_params* parser = arg->parser;
  • arg->num++;
  • if (arg->succ_block == 0) {
  •    arg->succ_block = NEW_BEGIN(0);
    
  •    arg->fail_block = NEW_BEGIN(0);
    
  • }
  • ID var = rb_intern3((const char *)name, name_end-name, arg->enc);
  • arg->succ_block = block_append(arg->succ_block,
  •    newline_node(node_assign(assignable(var,0),
    
  •        NEW_CALL(
    
  •          gettable(rb_intern("$~")),
    
  •          rb_intern("[]"),
    
  •          NEW_LIST(NEW_LIT(ID2SYM(var))))
    
  •        )));
    
  • arg->fail_block = block_append(arg->fail_block,
  •    newline_node(node_assign(assignable(var,0), NEW_LIT(Qnil))));
    
  • return 0;
    +}

+static NODE *
+reg_named_capture_assign_gen(struct parser_params* parser, VALUE
regexp, NODE *match)
+{

  • reg_named_capture_assign_t arg;
  • arg.parser = parser;
  • arg.enc = rb_enc_get(regexp);
  • arg.succ_block = 0;
  • arg.fail_block = 0;
  • arg.num = 0;
  • onig_foreach_name(RREGEXP(regexp)->ptr,
    reg_named_capture_assign_iter, (void*)&arg);
  • if (arg.num == 0)
  •    return match;
    
  • return
  •    block_append(
    
  •        newline_node(match),
    
  •        NEW_IF(gettable(rb_intern("$~")),
    
  •            block_append(
    
  •                newline_node(arg.succ_block),
    
  •                newline_node(
    
  •                    NEW_CALL(
    
  •                      gettable(rb_intern("$~")),
    
  •                      rb_intern("begin"),
    
  •                      NEW_LIST(NEW_LIT(INT2FIX(0)))))),
    
  •            block_append(
    
  •                newline_node(arg.fail_block),
    
  •                newline_node(
    
  •                    NEW_LIT(Qnil)))));
    

+}
+
static VALUE
reg_compile_gen(struct parser_params* parser, VALUE str, int options)
{

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32588] /(?…)/ =~ str assigns foo”
on Fri, 14 Dec 2007 08:38:56 +0900, Tanaka A. [email protected]
writes:

|e$B0J2<$N$h$&$Ke(B named capture e$B$N7k2L$r<+F0E*$KJQ?t$KBeF~$5$;$?e(B
|e$B$$$N$G$9$,$I$&$G$7$g$&$+!#e(B

e$B$(!<!"$I$&[email protected]$m$&$J$"!#e(B

|% ./ruby -e ’
|/(?\w+)/ =~ " foo "
|p word
|’
|“foo”
|
|e$BBeF~$,5/$3$k>r7o$O!"e(Bnamed capture e$B$r4^$_!“e(B#{} e$B$J$I$K$h$kF0Ee(B
|e$B$JE83+$N4^$^[email protected],I=8=$,e(B =~ e$B$N:8JU$K%j%F%i%k$H$7$FI=e(B
|e$B$l$?$H$-!"$G$9!#e(B
|
|e$B$J$
!”%^%C%A$7$J$+$C$?>l9g$Oe(B nil e$B$rBeF~$9$k$h$&$K$7$F$$$^$9!#e(B

e$B$3$N;[email protected]@-$H9gM}@-$O$"$k$H;W$$$^$9!#[email protected]$OG’e(B
e$B$a$k$N$G$9$,!"e(B

  • e$B:#$^$G$$$o$f$kBeF~0J30$G%m!<%+%kJQ?t$,EP>l$9$kJ8K!$O%V%m%Ce(B
    e$B%/%Q%i%a!<%?$7$+$J$/!"?7$7$$$b$N$rF3F~$9$k$N$ODq93$,$"$ke(B
    e$B$N$G$O$J$$$+!#$7$+$7!"[email protected]$NLdBj$O$J$$$N$G!"MW$9$k$K47e(B
    [email protected]$1$NLdBj$H$$$&5$$b$9$k!#e(B

  • e$B$"$kJQ?t$,%m!<%+%kJQ?t$+L50z?t$N%a%=%C%I8F$S=P$7$+$r6hJLe(B
    e$B$9$k$N$,$d$d:$Fq$K$J$k$N$G$O$J$$$+!#$7$+$7!":#$G$bJ8;zNse(B
    e$BCf$N<0E83+$NFbIt$KBeF~$,$"$l$P%j%F%i%kFb$b%A%’%C%/$9$kI,e(B
    e$BMW$,$"$k$N$G!"05E]E*$K:$Fq$K$J$k$H$^[email protected]$($J$$$h$&$J5$e(B
    e$B$b$9$k!#e(B

e$B$H$$$&$o$1$G!"B(7h$G$-$^$;$s!#FC$K%m!<%+%kJQ?t$NH=JL$,Fq$7$/e(B
e$B$J$ke(B(e$B$+$b$7$l$J$$e(B)e$B$N$O7|G0$G$9!#%?%$%_%s%0$r9M$($k$He(B1.9.1e$B$KF3e(B
e$BF~$9$k$N$OFq$7$$$+$b!#e(B

                            e$B$^$D$b$He(B e$B$f$-$R$me(B /:|)

In article [email protected],
Yukihiro M. [email protected] writes:

e$B$(!<!"$I$&[email protected]$m$&$J$"!#e(B

e$B$A$J$_$K!"$3$l$O:rF|:#F|$G;W$$IU$$$?$b$N$G$O$J$/!"0JA0$+$i$=e(B
e$B$&$7$?$$$H9M$($F$$$k$b$N$G$9!#e(B[ruby-dev:20725] e$B$H$+!#e(B

  • e$B:#$^$G$$$o$f$kBeF~0J30$G%m!<%+%kJQ?t$,EP>l$9$kJ8K!$O%V%m%Ce(B
    e$B%/%Q%i%a!<%?$7$+$J$/!"?7$7$$$b$N$rF3F~$9$k$N$ODq93$,$"$ke(B
    e$B$N$G$O$J$$$+!#$7$+$7!"[email protected]$NLdBj$O$J$$$N$G!"MW$9$k$K47e(B
    [email protected]$1$NLdBj$H$$$&5$$b$9$k!#e(B

e$B%m!<%+%kJQ?t$NF3F~$O$[$+$K$b$"$j$^$9!#e(B

  • e$B%a%=%C%I%Q%i%a!<%?e(B
  • for var in …
  • rescue exc => var

rescue e$B$N$O$$$DF3F~$5$l$^$7$?$C$1e(B? e$B:G=i$+$i$"$C$?$o$1$G$O$Je(B
e$B$$$h$&$K;W$$$^$9!#e(Brescue e$B$K$D$$$F$O?7$7$$$b$N$KBP$9$kDq93$Oe(B
e$B46$8$i$l$^$;$s$7!"?7$7$$$H$$$&[email protected]$1$GDq93$5$l$k$H$$$&$3$H$O$Je(B
e$B$$$N$G$O$J$$$G$7$g$&$+!#e(B

e$B$^$?!"%j%j!<%9$5$l$F$7$^$&[email protected]$NLdBj$,=P$k$N$G!"$<$R8=;~e(B
[email protected]$GF~$l$?$$$H;W$C$F$$$^$9!#e(B

  • e$B$"$kJQ?t$,%m!<%+%kJQ?t$+L50z?t$N%a%=%C%I8F$S=P$7$+$r6hJLe(B
    e$B$9$k$N$,$d$d:$Fq$K$J$k$N$G$O$J$$$+!#$7$+$7!":#$G$bJ8;zNse(B
    e$BCf$N<0E83+$NFbIt$KBeF~$,$"$l$P%j%F%i%kFb$b%A%’%C%/$9$kI,e(B
    e$BMW$,$"$k$N$G!"05E]E*$K:$Fq$K$J$k$H$^[email protected]$($J$$$h$&$J5$e(B
    e$B$b$9$k!#e(B

e$B$=$&$G$9$M$’!"$=$s$J$KFq$7$$$H$O46$8$J$$$N$G$9$,!"L>A0$rL\N)e(B
e$B$?$;$i$l$k$h$&$Ke(B (?…) e$B$Ge(B name
e$B$NA08e$K6uGr$rF~$l$i$l$k$he(B
e$B$&$K$9$k$H$$$&$N$O$I$&$G$9$+$M!#e(B (?< name >…) e$B$H$+!#e(B

e$B$H$$$&$o$1$G!"B(7h$G$-$^$;$s!#FC$K%m!<%+%kJQ?t$NH=JL$,Fq$7$/e(B
e$B$J$ke(B(e$B$+$b$7$l$J$$e(B)e$B$N$O7|G0$G$9!#%?%$%_%s%0$r9M$($k$He(B1.9.1e$B$KF3e(B
e$BF~$9$k$N$OFq$7$$$+$b!#e(B

e$B>e$K$b=q$-$^$7$?$,!"[email protected]$NLdBj$,B8:$7$J$$%?%$%%s%0$GF~$le(B
e$B$?$$$H;W$C$F$$$^$9!#e(B

e$B$"$H!"[email protected]$r=R$Y$k$He(B

if /(?…)/ =~ str
foo = $~[:foo]
end

e$B$He(B 3e$B2se(B foo e$B$H=q$+$J$$$H$$$1$J$$$N$,0J2<$N$h$&$Ke(B
1e$B2s$G:Q$`$he(B
e$B$&$K$J$k$3$H$G$9!#e(B

if /(?…)/ =~ str
end

e$B$=$7$F!"e(B$1, $2, … e$B$NHV9f$O?t$(4V0c$($F%P%0$N860x$K$J$k$N$Ge(B
e$BHr$1$?$$$N$G$9$,!"e(B

if /(…)/ =~ str
foo = $1
end

e$B$H$$$&$N$O>e5-$Ne(B 3e$B2s=q$+$J$1$l$P$$$1$J$$$N$KHf$Y$FC;$$$N$G!"e(B
$1 e$B$r;H$&M6OG$K6n$i$l$F$7$^$$$^$9!#e(B1e$B2s$G$9$`$J$i$=$C$A$N$[$&e(B
e$B$,C;$$$N$G4V0c$$$K$/$$$[$&$KM6F3$G$-$^$9!#e(B

e$B%P%0$r=P$7$K$/$$%9%?%$%k$r?d>)$9$k$H$$$&0UL#$G$b$<$RF~$l$?$$e(B
e$B$N$G$9$,!"$I$&$G$9$+$M!#e(B

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32597] Re: /(?…)/ =~ str assigns foo”
on Fri, 14 Dec 2007 18:17:17 +0900, Tanaka A. [email protected]
writes:

|> e$B$(!<!"$I$&[email protected]$m$&$J$"!#e(B
|
|e$B$A$J$_$K!"$3$l$O:rF|:#F|$G;W$$IU$$$?$b$N$G$O$J$/!"0JA0$+$i$=e(B
|e$B$&$7$?$$$H9M$($F$$$k$b$N$G$9!#e(B[ruby-dev:20725] e$B$H$+!#e(B

e$B$$$d!"$=$l$O$b$A$m$s>5CN$7$F$^$9!#$N$G!"Ds0F$5$l$?$3$H$=$N$be(B
e$B$N$K6C$-$O$J$$$G$9!#%?%$%_%s%0E*$K$O$A$g$C$H$S$C$/$j$7$^$7$?e(B
e$B$1$I!#e(B

|e$B%m!<%+%kJQ?t$NF3F~$O$[$+$K$b$"$j$^$9!#e(B
|
|* e$B%a%=%C%I%Q%i%a!<%?e(B
|* for var in …
|* rescue exc => var
|
|rescue e$B$N$O$$$DF3F~$5$l$^$7$?$C$1e(B? e$B:G=i$+$i$"$C$?$o$1$G$O$Je(B
|e$B$$$h$&$K;W$$$^$9!#e(Brescue e$B$K$D$$$F$O?7$7$$$b$N$KBP$9$kDq93$Oe(B
|e$B46$8$i$l$^$;$s$7!"?7$7$$$H$$$&[email protected]$1$GDq93$5$l$k$H$$$&$3$H$O$Je(B
|e$B$$$N$G$O$J$$$G$7$g$&$+!#e(B

e$B$^$"!"$=$&$J$s$G$9$1$I!"[email protected],I=8=%j%F%i%k$Ne(B
named capturee$B$O!"$J$s$H$$$&$N$+!VBeF~$7$F$k46!W$,>/$J$/$J$$e(B
e$B$G$9$+!#e(B

|e$B$^$?!"%j%j!<%9$5$l$F$7$^$&[email protected]$NLdBj$,=P$k$N$G!"$<$R8=;~e(B
|[email protected]$GF~$l$?$$$H;W$C$F$$$^$9!#e(B

e$B5$;}$A$OJ,$+$j$^$9!#e(B

|e$B$"$H!"[email protected]$r=R$Y$k$He(B
|
|if /(?…)/ =~ str
| foo = $~[:foo]
|end
|
|e$B$He(B 3e$B2se(B foo e$B$H=q$+$J$$$H$$$1$J$$$N$,0J2<$N$h$&$Ke(B 1e$B2s$G:Q$`$he(B
|e$B$&$K$J$k$3$H$G$9!#e(B
|
|if /(?…)/ =~ str
|end

[email protected]$Oe(B(e$B4V0c$$$K$/$$J}8~$KM6F3$9$k%a%j%C%H$b4^$a$Fe(B)e$BG’$a$^$9!#e(B

e$B$H$O$$$(!"e(B

e$B$,5$$K$J$k$s$G$9$h$M!#0EL[$N5sF0$O7k9=7y$o$l$k$H$$$&$3$H$b$"e(B
e$B$j$^$9$7!#e(B

e$B$&!<$s!#e(B

                            e$B$^$D$b$He(B e$B$f$-$R$me(B /:|)

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32605] Re: /(?…)/ =~ str assigns foo”
on Sat, 15 Dec 2007 06:56:15 +0900, Tanaka A. [email protected]
writes:

|> * e$B>e$G=R$Y$?BeF~46$N$J$5e(B
|> * named capturee$B$9$Y$F$G$O$J$/%j%F%i%k$N$_e(B (e$B$$$d!"$=$&$8$ce(B
|> e$B$J$$$H!"$H$F$b;H$([email protected]$1$Ie(B) e$B$H$$$&FC<[email protected](B
|>
|> e$B$,5$$K$J$k$s$G$9$h$M!#0EL[$N5sF0$O7k9=7y$o$l$k$H$$$&$3$H$b$"e(B
|> e$B$j$^$9$7!#e(B
|
|e$B$`$7$me(B $~ e$B$X$N0EL[[email protected]<(E*$K$J$j$^$9!#$=$N0UL#$Ge(B
|e$B$O!“e(B$~ e$B$X$NBeF~$KHf$Y$FJQ?tL>$,$A$c$s$H=q$$$F$”$k$V$s!"BeF~e(B
|e$B46$OA}2C$7$F$$$k$N$G$O$J$$$G$7$g$&$+!#e(B

[email protected]$OF10U$7$^$9$,!"e(B$~e$B$O!VFC<lJQ?t!W$G$9$+$i!#e(B

[email protected]~>e$G9M$($k$H!"5$IU$+$:$K%m!<%+%kJQ?t$,GK2u$5$l$k$3e(B
e$B$H$r?4G[$7$F$$$k$h$&$J5$$,$7$FMh$^$7$?!#e(B-we$B%*%W%7%g%s$G!“e(B
named capturee$B$HF1L>$N%m!<%+%kJQ?t$,$9$G$KDj5A$5$l$F$?$i7Y9pe(B
e$B$9$k!”$H$$$&>r7o$D$-$GG’$a$F$b$h$$$+$b$7$l$^$;$s!#e(B

In article [email protected],
Yukihiro M. [email protected] writes:

[email protected]~>e$G9M$($k$H!"5$IU$+$:$K%m!<%+%kJQ?t$,GK2u$5$l$k$3e(B
e$B$H$r?4G[$7$F$$$k$h$&$J5$$,$7$FMh$^$7$?!#e(B-we$B%*%W%7%g%s$G!“e(B
named capturee$B$HF1L>$N%m!<%+%kJQ?t$,$9$G$KDj5A$5$l$F$?$i7Y9pe(B
e$B$9$k!”$H$$$&>r7o$D$-$GG’$a$F$b$h$$$+$b$7$l$^$;$s!#e(B

e$B$=$N$h$&$K$7$F%3%_%C%H$7$^$7$?!#e(B

In article [email protected],
Yukihiro M. [email protected] writes:

e$B$$$d!"$=$l$O$b$A$m$s>5CN$7$F$^$9!#$N$G!"Ds0F$5$l$?$3$H$=$N$be(B
e$B$N$K6C$-$O$J$$$G$9!#%?%$%_%s%0E*$K$O$A$g$C$H$S$C$/$j$7$^$7$?e(B
e$B$1$I!#e(B

e$B$^$!!"D>A0$J$N$O3N$+$G$9$M!#e(B

e$B$^$"!"$=$&$J$s$G$9$1$I!"[email protected],I=8=%j%F%i%k$Ne(B
named capturee$B$O!"$J$s$H$$$&$N$+!VBeF~$7$F$k46!W$,>/$J$/$J$$e(B
e$B$G$9$+!#e(B

e$BCf?H$rD4$Y$?$iCM$,%P%$%s%I$5$l$k$N$OIaDL$G$O$J$$$G$7$g$&$+!#e(B
$1 e$B$H$+$b$=$&$G$9$7!"B>[email protected]$Ge(B Haskell e$BEy$N%Q%?!<%s%^%C%A$be(B
e$B$=$&$G$9$,!#e(B

|e$B$^$?!"%j%j!<%9$5$l$F$7$^$&[email protected]$NLdBj$,=P$k$N$G!"$<$R8=;~e(B
|[email protected]$GF~$l$?$$$H;W$C$F$$$^$9!#e(B

e$B5$;}$A$OJ,$+$j$^$9!#e(B

e$B$3$N%?%$%_%s%0$rF($9$H2?G/$bF~$l$i$l$J$$$3$H$K$J$j$=$&$J$N$,e(B
e$B7y$G$9!#$=$l$H$b!"e(B1.9.1 e$BD>8e$KF~$l$F$b$$$$$G$9$+e(B?

e$B$,5$$K$J$k$s$G$9$h$M!#0EL[$N5sF0$O7k9=7y$o$l$k$H$$$&$3$H$b$"e(B
e$B$j$^$9$7!#e(B

e$B$`$7$me(B $~ e$B$X$N0EL[[email protected]<(E*$K$J$j$^$9!#$=$N0UL#$Ge(B
e$B$O!“e(B$~ e$B$X$NBeF~$KHf$Y$FJQ?tL>$,$A$c$s$H=q$$$F$”$k$V$s!"BeF~e(B
e$B46$OA}2C$7$F$$$k$N$G$O$J$$$G$7$g$&$+!#e(B

e$B%j%F%i%k$N$_$J$N$O!"%m!<%+%kJQ?t$,e(B lexical scope e$B$G$"$k$H$$e(B
e$B$&@-e$=$N$h$&$K$7$+%G%6%$%s$G$-$^$;$s!#JQ?tL>$He(B =~ e$B$Ne(B =
e$B$H$$$&J8;z$NAH$_9g$o$;$GBeF~$H9M$($k$H$$$&$N$O$I$&$G$7$g$&e(B?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs