From: Jamis Buck Date: 2003-12-18T04:30:22+09:00 Subject: Re: gsub question Daniel Bretoi wrote: >perl -e '$a="hello hi hello";$a =~ s/(\w+)/$1a/g;print $a; ' >helloa hia helloa > >ruby -e 'a="hello hi hello";a.gsub!(/(\w+)/,"#{$1}a"); puts a' >a a a > >Why? I'm expecting the same result as in perl. > > Because the $1 isn't defined at the time the replacement string is parsed. Instead, use \1: ruby -e 'a="hello hi hello";a.gsub!(/(\w+)/, '\1a');puts a' hello hia helloa -- Jamis Buck jgb3@email.byu.edu ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'