Wrapped String#gsub

e$B$3$s$K$A$O!"KQe(B e$B<G0u$G$9!#e(B

e$B<!$N$h$&$Ke(B gsube$B$r%i%C%T%s%C%0$7$Fe(B
e$B<B9T$9$k$H!"$A$g$C$H0U30$J7k2L$,$G$^$9$,!"[email protected]$7$$$G$7$g$&$+!#e(B

e$B4*[email protected]$C$?$i$9$_$^$;$s!#e(B

$ cat test2.rb
class String
def wrap_gsub(*a, &b)
self.gsub(*a, &b)
puts “in wrapper ? #{$&}”
end
end

puts “wrapper”
“abc”.wrap_gsub(/([abc]+)/n) {|x| p $&}
puts “and out of wrapper - #{$&}”
puts “=======”
puts “original”
“abc”.gsub(/([abc]+)/n) {|x| p $&}

e$B7k2L$G$9!#e(B1.8/1.9 e$BF1$8$G$9!#e(B

$ ruby test2.rb
wrapper
nil
in wrapper ? abc
and out of wrapper -

original
“abc”
$ ruby --version
ruby 1.9.0 (2008-01-22 revision 0) [i686-linux]
[email protected] ~/Development/ruby-1.8.6 $ ruby18 test2.rb
wrapper
nil
in wrapper ? abc
and out of wrapper -

original
“abc”
$ ruby18 --version
ruby 1.8.6 (2007-12-03 patchlevel 113) [i686-linux]

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

Park Ji-In wrote:

e$B$3$s$K$A$O!"KQe(B e$B<G0u$G$9!#e(B

e$B<!$N$h$&$Ke(B gsube$B$r%i%C%T%s%C%0$7$Fe(B
e$B<B9T$9$k$H!"$A$g$C$H0U30$J7k2L$,$G$^$9$,!"[email protected]$7$$$G$7$g$&$+!#e(B

e$B4*[email protected]$C$?$i$9$_$^$;$s!#e(B

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

$ cat test2.rb
class String
def wrap_gsub(*a, &b)
self.gsub(*a, &b)
puts “in wrapper ? #{$&}” # <= e$B$3$l$N7k2L$G$"$ke(B nil e$B$,JV$j$^$9e(B
end
end

e$B$J$N$G!"e(B

$ cat test2.rb
class String
def wrap_gsub(*a, &b)
self.gsub(*a, &b)
puts “in wrapper ? #{$&}”
self
end
end

e$B$?$H$($P$3$s$J46$8$K$7$^$7$g$&e(B

e$BKQe(B e$B<G0u$G$9!#e(B
e$B$9$_$^$;$s!#@bL@$,B-$j$J$+$C$?$N$G?=$7Lu$4$6$$$^$;$s!#e(B

e$B2~$a$F=q$$$F$_$^$9!#e(B

class String
def wrap_gsub(*a, &b)
self.gsub(*a, &b)
end
end

puts “wrapper”
“abc,abc”.wrap_gsub(/([abc]+)/) { puts $1 }
puts “original”
“abc,abc”.gsub(/([abc]+)/) { puts $1 }

– e$B7k2Le(B –

wrapper
nil
nil
original
“abc”
“abc”

e$B$G$9!#e(Bgsube$B$re(Bblocke$BIU$-$G8F$V$H$-$O8!:w$G$-$?$b$8$l$D$Oe(B
$1, $2e$B$J$I$GEO$9$H=q$$$F$"$j$^$7$?!#e(B

e$B$,!"$3$s$JIw$Ke(Bblocke$B$rEO$7$Fe(Bdeligatore$B8+$?$$$J??;w$re(B
e$B$9$k$3$H$O=PMh$J$$$G$7$g$&$+!#e(B

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

Park Ji-In wrote:

original
“abc”
“abc”

e$B$G$9!#e(Bgsube$B$re(Bblocke$BIU$-$G8F$V$H$-$O8!:w$G$-$?$b$8$l$D$Oe(B
$1, $2e$B$J$I$GEO$9$H=q$$$F$"$j$^$7$?!#e(B

e$B$,!"$3$s$JIw$Ke(Bblocke$B$rEO$7$Fe(Bdeligatore$B8+$?$$$J??;w$re(B
e$B$9$k$3$H$O=PMh$J$$$G$7$g$&$+!#e(B

e$B$(!<$C$H!"$?$V$s!"e(B

class String
def wrap_gsub(a)
self.gsub(a){ yield $1}
end
end

e$B$H$+e(B

class String
def wrap_gsub(*a)
self.gsub(*a)
end
end

e$B$G$7$g$&$+!#e(B

e$B$3$s$P$s$O!"9uED$R$i$/[email protected]$$$^$9!#e(B

e$B%a%=%C%I$KEO$7$?%V%m%C%/$Ne(B$1,
$2e$B$O!"$=$N%V%m%C%/$r:n$C$?%9%3!<%W$N$b$N$N$h$&$G$9!#e(B


#!/usr/bin/ruby
class String
def wrap_gsub(re, &b)
self=~re # =>
e$B$3$N%^%C%A$N%G!<%?$Oe(Bbe$B$NCf$Ne(B$1e$B$KH?1G$5$l$J$$e(B
self.gsub(re, &b)
end
end

“abc,abc”=~/([a]+)/
“abc,abc”.wrap_gsub(/([abc]+)/) { puts $1 } # =>
e$B>e$N9T$N%^%C%A$N%G!<%?$,e(B$1e$B$KF~$ke(B

e$B<B9T$9$k$He(B

~$ ruby -v test.rb
ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]
a
a

e$B$H$J$j$^$9!#e(B

e$B%V%m%C%/$NCf$G%V%m%C%/$r<B9T$9$k%a%=%C%I$N%^%C%A$N%G!<%?$r;H$$$?$$>l9g$G$9$,!"%V%m%C%/$N%Q%i%a!<%?e(B
e$B$Ke(B$&e$B$rEO$9$N$O$$$+$,$G$7$g$&$+e(B?

---- e$B%=!<%9e(B
#!/usr/bin/ruby
class String
def wrap_gsub2(*a)
self.gsub(*a){yield($~) if block_given?}
end
end

“abc,abc”.wrap_gsub2(/(a)(bc)/) {|m| puts m[0], m[1], m[2] }

---- e$B=PNOe(B
$ ruby -v test.rb
ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]
abc
a
bc
abc
a
bc

Park Ji-In e$B$5$s$O=q$-$^$7$?e(B:

e$BKQe(B e$B<G0u$G$9!#e(B

2008/1/26, KURODA Hiraku [email protected]:

“abc,abc”.wrap_gsub2(/(a)(bc)/) {|m| puts m[0], m[1], m[2] }

e$B2?$+$d$d$3$7$/<j$rF~$i$J$1$l$P$J$i$J$$$h$&$G$9$M!#e(B
e$B$=$l$,=PMh$k;~$O$=$l$GNI$$$H;W$$$^$9$,!"e(B

DelegateClasse$B$bF1$87k2L$,$G$^$7$?!#e(B

require ‘delegate’

class Merong < DelegateClass(String)
def initialize(str)
@mystr = str
super(@mystr)
end
end

m = Merong.new(‘abc’)
s = m.gsub(/([abc]+)/) {$1.upcase}
puts s

e$B7k2Le(B

(eval):3:in __send__': undefined methodupcase’ for nil:NilClass
(NoMethodError)
from (eval):3:in `gsub’
from delegate_test.rb:11

e$B$3$l$O%P%[email protected]$HG’$a$i$l$k$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