Block parameter of String#gsub

[email protected]@%$G$9!#e(B

Ruby e$B%9%l%C%I$G=P$F$$$?OCBj$J$N$G$9$,!“8=:_$Ne(B String#gsub
e$B$,%V%m%C%/$r<ue(B
e$B$1<h$C$?;~!”%V%m%C%/$K$Oe(B $& e$B$,EO$5$l$F$$$^$9!#e(B

“abcdefg”.gsub(/(.).(.)/){|m|p m; m}
“abc”
“def”
=> “abcdefg”

[email protected]$H!";ve%V%m%C%/%Q%i%a!<%?$O;H$$J*$K$J$i$J$$$N$G!"e(BMatchData
e$B$rEOe(B
e$B$9$h$&$K$7$^$;$s$+!#e(B

“abcdefg”.gsub(/(.).(.)/){|m|p m; m}
#<MatchData “abc” 1:“a” 2:“c”>
#<MatchData “def” 1:“d” 2:“f”>
=> “abcdefg”

[email protected]$G$9$,e(B m.to_s e$B$Oe(B e$B:#$^$G$Ne(B $&
e$B$H0lCW$9$k$N$G!“e(B
“abcdefg”.gsub(/(.).(.)/){|m| m+”,"} e$B$O$5$9$,$K%(%i!<$,=P$^$9$,!"e(B
“abcdefg”.gsub(/(.).(.)/){|m| “%s,” % m}
e$B$J$i:#$^$GDL$jF0$-$^$9$7!“e(B
e$B$=$b$=$b$*$=$i$/$[$H$s$I$N8=:_$N%3!<%I$O!“e(B
“abcdefg”.gsub(/(.).(.)/){$&+”,”} e$B$G$7$g$&!#e(B

— string.c (revision 15354)
+++ string.c (working copy)
@@ -2739,7 +2739,7 @@ rb_str_sub_bang(int argc, VALUE *argv, V
char *p = RSTRING_PTR(str); long len = RSTRING_LEN(str);

        rb_match_busy(match);
  •       repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, 
    

match)));

  •       repl = rb_obj_as_string(rb_yield(match));
          str_mod_check(str, p, len);
          str_frozen_check(str);
          rb_backref_set(match);
    

@@ -2876,7 +2876,7 @@ str_gsub(int argc, VALUE *argv, VALUE st
regs = RMATCH(match)->regs;
if (iter) {
rb_match_busy(match);

  •       val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, 
    

match)));

  •       val = rb_obj_as_string(rb_yield(match));
          str_mod_check(str, sp, slen);
          if (bang) str_frozen_check(str);
          if (val == dest) {  /* paranoid check [ruby-dev:24827] */

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

In message “Re: [ruby-dev:33548] block parameter of String#gsub”
on Thu, 31 Jan 2008 15:49:51 +0900, “NARUSE, Yui”
[email protected] writes:

|Ruby e$B%9%l%C%I$G=P$F$$$?OCBj$J$N$G$9$,!“8=:_$Ne(B String#gsub e$B$,%V%m%C%/$r<ue(B
|e$B$1<h$C$?;~!”%V%m%C%/$K$Oe(B $& e$B$,EO$5$l$F$$$^$9!#e(B

|[email protected]$H!";ve%V%m%C%/%Q%i%a!<%?$O;H$$J*$K$J$i$J$$$N$G!"e(BMatchData e$B$rEOe(B
|e$B$9$h$&$K$7$^$;$s$+!#e(B

e$B$3$l$O5$$K$7$F$$$?$N$G$9$,!“[email protected]$N$3$H$,$”$k$N$Gm4m0$7$F$$e(B
e$B$^$7$?!#e(B

|[email protected]$G$9$,e(B m.to_s e$B$Oe(B e$B:#$^$G$Ne(B $& e$B$H0lCW$9$k$N$G!“e(B
|“abcdefg”.gsub(/(.).(.)/){|m| m+”,"} e$B$O$5$9$,$K%(%i!<$,=P$^$9$,!"e(B
|“abcdefg”.gsub(/(.).(.)/){|m| “%s,” % m} e$B$J$i:#$^$GDL$jF0$-$^$9$7!“e(B
|e$B$=$b$=$b$*$=$i$/$[$H$s$I$N8=:_$N%3!<%I$O!“e(B
|“abcdefg”.gsub(/(.).(.)/){$&+”,”} e$B$G$7$g$&!#e(B

e$B$&!<$s!"$=$3$^[email protected]$l$k$N$+$J$"!#B>$N?M$N0U8+$rJ9$-$?$$$He(B
e$B$3$m$G$9!#e(B

e$B;d$,9M$($F$$$?e(B(e$B$1$I!“e(B1.9.0e$B$K$O4V$K9g$o$J$+$C$?e(B)e$B%”%$%G%#%"$O!"e(B
sube$B$KBP1~$9$ke(Bse$B!“e(Bgsube$B$KBP1~$9$ke(Bsg(e$B$^$?$Oe(Bgs)e$B%a%=%C%I$re(BStringe$B%/e(B
e$B%i%[email protected]_$7!”$3$A$i$G$Oe(BMatchDatae$B$rM?$($k$H$$$&$b$N$G$9!#e(B

e$B$I$&$$$&$N$,NI$$$G$7$g$&$M!#e(B

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

[email protected]@%$G$9!#e(B

Yukihiro M. wrote:

e$B$3$m$G$9!#e(B

e$B;d$,9M$($F$$$?e(B(e$B$1$I!“e(B1.9.0e$B$K$O4V$K9g$o$J$+$C$?e(B)e$B%”%$%G%#%"$O!"e(B
sube$B$KBP1~$9$ke(Bse$B!“e(Bgsube$B$KBP1~$9$ke(Bsg(e$B$^$?$Oe(Bgs)e$B%a%=%C%I$re(BStringe$B%/e(B
e$B%i%[email protected]_$7!”$3$A$i$G$Oe(BMatchDatae$B$rM?$($k$H$$$&$b$N$G$9!#e(B

e$B$*$)!"$=$l$O$$$$$H;W$$$^$9!#e(B
String#scan e$B$N%V%m%C%/%Q%i%a!<%?$Ne(B
MatchDatae$BHG$bJ;$;$F$[$7$$$G$9$M!#e(B
s, sg, sc [email protected]$H$=$3$O$+$H$J$/6&[email protected]$b$"$k!&!&!&$+$b!#e(B

e$BA0ED$G$9!#e(B

On 01/31/08 23:32, Yukihiro M. wrote:

|[email protected]$G$9$,e(B m.to_s e$B$Oe(B e$B:#$^$G$Ne(B $& e$B$H0lCW$9$k$N$G!“e(B
|“abcdefg”.gsub(/(.).(.)/){|m| m+”,"} e$B$O$5$9$,$K%(%i!<$,=P$^$9$,!"e(B
|“abcdefg”.gsub(/(.).(.)/){|m| “%s,” % m} e$B$J$i:#$^$GDL$jF0$-$^$9$7!“e(B
|e$B$=$b$=$b$*$=$i$/$[$H$s$I$N8=:_$N%3!<%I$O!“e(B
|“abcdefg”.gsub(/(.).(.)/){$&+”,”} e$B$G$7$g$&!#e(B

e$B$&!<$s!"$=$3$^[email protected]$l$k$N$+$J$"!#B>$N?M$N0U8+$rJ9$-$?$$$He(B
e$B$3$m$G$9!#e(B

e$B8D?ME*$K$Oe(Bstr.gsub(/[A-Z]/) {|s| "" +
s.downcase}e$B$
$?$$$J%3!<%I$Oe(B
e$B7k9==q$$$F$^$9$M!#e(B
grep 'gsub.*{
*|'e$B$H$+$7$F$_$k$H!“I8=`E:IU%i%$%V%i%j$K$b$$$/$D$+F0$+e(B
e$B$J$/$J$k%3!<%I$,$”$j$=$&$G$9!#e(B

e$B;d$,9M$($F$$$?e(B(e$B$1$I!“e(B1.9.0e$B$K$O4V$K9g$o$J$+$C$?e(B)e$B%”%$%G%#%"$O!"e(B
sube$B$KBP1~$9$ke(Bse$B!“e(Bgsube$B$KBP1~$9$ke(Bsg(e$B$^$?$Oe(Bgs)e$B%a%=%C%I$re(BStringe$B%/e(B
e$B%i%[email protected]_$7!”$3$A$i$G$Oe(BMatchDatae$B$rM?$($k$H$$$&$b$N$G$9!#e(B

se$B$de([email protected]$H$A$g$C$HC;$+$9$.$^$;$s$+e(B?

e$B1sF#$H?=$7$^$9!#e(B

08/02/01 e$B$Ke(B NARUSE, Yui[email protected]
e$B$5$s$O=q$-$^$7$?e(B:

|[email protected]$G$9$,e(B m.to_s e$B$Oe(B e$B:#$^$G$Ne(B $& e$B$H0lCW$9$k$N$G!“e(B
|“abcdefg”.gsub(/(.).(.)/){|m| m+”,"} e$B$O$5$9$,$K%(%i!<$,=P$^$9$,!"e(B
|“abcdefg”.gsub(/(.).(.)/){|m| “%s,” % m} e$B$J$i:#$^$GDL$jF0$-$^$9$7!“e(B
|e$B$=$b$=$b$*$=$i$/$[$H$s$I$N8=:_$N%3!<%I$O!“e(B
|“abcdefg”.gsub(/(.).(.)/){$&+”,”} e$B$G$7$g$&!#e(B

[email protected]$C$A$c$&$H!“e(BMatchData
e$B$,$[$7$$$H$-$N8=:_$N%3!<%[email protected]$C$Fe(B
“abcdefg”.gsub(/(.).(.)/){$1+”,"+$2}
e$B$J$N$G!";EMM$rJQ$($kM}M3$H$7$F$O<e$$$H;W$$$^$7$?!#e(B

e$BLdBj$O$d$O$j85$N%9%l%C%I$K$"$C$?$h$&$K!“e(Bscan e$B$He(B gsub
e$B$N%V%m%C%/e(B
e$B%Q%i%a!<%?$,0[$J$C$F$$$F$o$+$j$K$/[email protected][email protected]$H;W$$$^$9!#$I$&$;;EMM$re(B
e$BJQ$($k$J$i$Pe(B MatchData
e$B$K$=$m$($A$c$*$&!”$H$$$&$N$O;[email protected]$G$9!#e(B

e$B;d$,9M$($F$$$?e(B(e$B$1$I!“e(B1.9.0e$B$K$O4V$K9g$o$J$+$C$?e(B)e$B%”%$%G%#%"$O!"e(B
sube$B$KBP1~$9$ke(Bse$B!“e(Bgsube$B$KBP1~$9$ke(Bsg(e$B$^$?$Oe(Bgs)e$B%a%=%C%I$re(BStringe$B%/e(B
e$B%i%[email protected]_$7!”$3$A$i$G$Oe(BMatchDatae$B$rM?$($k$H$$$&$b$N$G$9!#e(B

e$B$*$)!"$=$l$O$$$$$H;W$$$^$9!#e(B
String#scan e$B$N%V%m%C%/%Q%i%a!<%?$Ne(B MatchDatae$BHG$bJ;$;$F$[$7$$$G$9$M!#e(B
s, sg, sc [email protected]$H$=$3$O$+$H$J$/6&[email protected]$b$"$k!&!&!&$+$b!#e(B

[email protected]?qH?<M$G!V5-9f2=$7$9$.!W$HH?BP$7$?$/$J$j$^$7$?$,!"$=$l0J30$Ne(B
e$BH?BPM}M3$O;W$$$D$-$^$;$s$G$7$?!#5U$K;[email protected]}M3$Oe(B

  • unix e$BJ82=$G$O!Ve(Bs e$B0lJ8;ze(B =
    e$BCV49!W$H$$$&%3%s%;%s%5%9$,$"$k$+$be(B
  • String e$B$N%$%s%9%?%s%9%a%=%C%[email protected]$+$i%0%m!<%P%k$K$O1F6A$,$J$$e(B
  • e$B85$Ne(B sub e$B$de(B gsub
    [email protected]$C$FL>[email protected]$1$8$c0UL#$,$o$+$i$J$$e(B
  • e$BHs8_49$G$O$J$/?75!G=$J$N$G:#$+$i$G$bF2!9$H$d$l$ke(B

e$B$H!"$$$C$Q$$;W$$$D$-$^$7$?!#e(B

e$B$H$O$$$(!“e(BPerl e$B$,$$$d$Ge(B Ruby e$B%f!<%6$K$J$C$??M!9e(B
(e$B4^$`;de(B) e$B$O2a>j$Je(B
e$B5-9f2=$K$ODq93$,$”$k$H;W$$$^$9!#e(B
e$B;d$N0u>]$O!"!Ve(Bgsub e$B$,FsJ8;z$K$J$k2ACM$O$"$k$+$bCN$l$J$$$1$Ie(B
sub e$B$,e(B
e$B0lJ8;z$K$J$k$[$I$N2ACM$O46$8$J$$!W$H$$$&46$8$G$9!#e(B

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

In message “Re: [ruby-dev:33557] Re: block parameter of String#gsub”
on Fri, 1 Feb 2008 12:34:34 +0900, Shugo M. [email protected]
writes:

|> e$B;d$,9M$($F$$$?e(B(e$B$1$I!“e(B1.9.0e$B$K$O4V$K9g$o$J$+$C$?e(B)e$B%”%$%G%#%"$O!"e(B
|> sube$B$KBP1~$9$ke(Bse$B!“e(Bgsube$B$KBP1~$9$ke(Bsg(e$B$^$?$Oe(Bgs)e$B%a%=%C%I$re(BStringe$B%/e(B
|> e$B%i%[email protected]_$7!”$3$A$i$G$Oe(BMatchDatae$B$rM?$($k$H$$$&$b$N$G$9!#e(B
|
|se$B$de([email protected]$H$A$g$C$HC;$+$9$.$^$;$s$+e(B?

e$B$=$&$+$b$7$l$^$;$s!#[email protected]!“e(Bakre$BM}[email protected]$K$h$k$H$h$jK>$^$7$$J}$rC;e(B
e$B$/$9$kJ}$,NI$$$o$1$G!#$h$jNI$$L>A0$NDs0F$,$”$l$P4?7^$7$^$9!#e(B

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

e$B$=$&$+$b$7$l$^$;$s!#[email protected]!“e(Bakre$BM}[email protected]$K$h$k$H$h$jK>$^$7$$J}$rC;e(B
e$B$/$9$kJ}$,NI$$$o$1$G!#$h$jNI$$L>A0$NDs0F$,$”$l$P4?7^$7$^$9!#e(B

e$B:#2s$N>l9g!"C;$/$9$kI,MW$O$J$/$F!"D9$/$J$1$l$P=<J,[email protected]$H;W$$$^e(B
e$B$9!#e(B

MatchData e$B$G$"$k$3$H$N;H$$$d$9$5<+?H$K$h$kM6F30J>e$K0U?^E*$Ke(B
e$BM6F3$9$kI,[email protected]$O$J$$$s$8$c$J$$$G$7$g$&$+!#:#$Ne(B sub, gsub e$B$Ge(B
e$B$b$H$/$KLdBj$,H/@8$7$d$9$$$H$$$&$3$H$O$J$$$G$9$7!#e(B

s, sg, gs [email protected]$C$F$$$k$3$H$KHf$Y$FC;$/$7$9$.$F$$$F!"e(B
MatchData e$B$K$h$j2r7h$9$kLdBj$KHf$Y$FL>A0$N$o$+$j$K$/$5$NLdBje(B
e$B$NEY9g$N$[$&$,Bg$-$/$J$C$F$$$k$h$&$K;W$$$^$9!#e(B

e$B$^$!!"$`$7$m!"$$$$L>A0$,8+$D$+$i$J$$7k2L!“7k6Ie(B gsub, sub e$B$re(B
e$B$”$($FHs8_49$KJQ99$9$k$N$,e(B Ruby e$B$C$]$$$H$$$&5$$b$7$^$9$,!#$be(B
[email protected]$KBQ$($i$l$k$N$G$"$l$P!“3P$($k$3$H$NNL$,A}$($J$$$He(B
e$B$$$&[email protected]$,$”$j$^$9$7!#e(B

e$B1J0f!wCNG=!%6e9)Bg$G$9!%e(B

From: Tanaka A. [email protected]
Subject: [ruby-dev:33568] Re: block parameter of String#gsub
Date: Sat, 2 Feb 2008 16:10:18 +0900
Message-ID: [email protected]

MatchData e$B$G$"$k$3$H$N;H$$$d$9$5<+?H$K$h$kM6F30J>e$K0U?^E*$Ke(B
e$BM6F3$9$kI,[email protected]$O$J$$$s$8$c$J$$$G$7$g$&$+!#:#$Ne(B sub, gsub e$B$Ge(B
e$B$b$H$/$KLdBj$,H/@8$7$d$9$$$H$$$&$3$H$O$J$$$G$9$7!#e(B

s, sg, gs [email protected]$C$F$$$k$3$H$KHf$Y$FC;$/$7$9$.$F$$$F!"e(B
MatchData e$B$K$h$j2r7h$9$kLdBj$KHf$Y$FL>A0$N$o$+$j$K$/$5$NLdBje(B
e$B$NEY9g$N$[$&$,Bg$-$/$J$C$F$$$k$h$&$K;W$$$^$9!#e(B

(e$B%%=%C$He(B) subm, gsubm e$B$H$+!%!%!%e(B
e$B%4%a%s%J%5%$!%;d$K$OL>A0$N%;%s%9$,$"$j$^$;$s!%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