Help: Improper use of gsub?

All-

The code below correctly creates one copy of file “template.txt” for each line in the file “packages.txt”, and names each copy with the text in field[0] in each line.

For each copy, I am trying to replace the string “<release_num>” with the file object fs (a very short file). However, the “<release_num>” string is not bing replaced. Am I making incorrect use of the gsub method (in 2nd line from bottom)? If so, please suggest a correction.

content = File.new(“template1.txt”).read
fs = File.new(“fiddle.sticks”).read
IO.foreach(“packages.txt”) { |x|
field = x.chop.split(’:’)
content2 = content
content2.gsub(/<release_num>/, fs)
File.new(field[0],“w+”).write(content2)
}

Thanks.

-Kurt Euler

Hi,

content2.gsub(/<release_num>/, fs)

try calling gsub! instead of gsub as otherwise
content2 does not get changed.

irb(main):002:0> s.gsub(/<release_num>/, “foo”)
“foo”
irb(main):003:0> s
“<release_num>”
irb(main):004:0> s.gsub!(/<release_num>/, “foo”)
“foo”
irb(main):005:0> s
“foo”

Hope this helps,
Armin

···

Am Samstag, 8. Juni 2002 08:06 schrieben Sie:

All-

The code below correctly creates one copy of file “template.txt” for each
line in the file “packages.txt”, and names each copy with the text in
field[0] in each line.

For each copy, I am trying to replace the string “<release_num>” with the
file object fs (a very short file). However, the “<release_num>” string is
not bing replaced. Am I making incorrect use of the gsub method (in 2nd
line from bottom)? If so, please suggest a correction.

content = File.new(“template1.txt”).read
fs = File.new(“fiddle.sticks”).read
IO.foreach(“packages.txt”) { |x|
field = x.chop.split(‘:’)
content2 = content
content2.gsub(/<release_num>/, fs)
File.new(field[0],“w+”).write(content2)
}

Thanks.

-Kurt Euler


Armin.


Armin Roehrl, http://www.approximity.com
Training, Development and Mentoring
OOP, XP, Java, Ruby, Smalltalk, Datamining, Parallel computing, Webservices
Koautor des ersten dt. Rubybuches Mitom Tv Bóng Đá Trực Tiếp - Kênh Ttbđ Mitomtv Không Giật Lag-Full Hd Blv
Naechstes Rubyseminar: 9/9-11/9 in Hamburg.

Don’t work hard, work smart.