From: Todd Benson Date: 2008-04-09T04:28:06+09:00 Subject: Re: capitalizing words On Tue, Apr 8, 2008 at 1:53 PM, Peter Bailey wrote: > Hi, > I need to capitalize the words in a string I find in XML files. > > The string that's in (.*) below is what I need to change. I just want to > capitalize the first letter of each word in the string. > > I'm trying this, in a test: > > Dir.chdir("C:/users/pb4072/documents") > file = File.read("test1.txt") > file.gsub(/^(.*)<\/emph>/) do |match| > array = $1.split > array.each do |word| > word.capitalize! > end > newfile = File.open("c:/users/pb4072/documents/test1.txt", "w") { |f| > f.print array } > end > > And, I'm getting this: > > #(.*)<\/emph>theQuickBrownFoxJumpedOverTheLazyDog. > > I want this: > > The Quick Brown Fox Jumped Over The > Lazy Dog.<\/emph>/ > > > Thanks, > Peter I don't know what the original text looks like in test1.txt, but this might point you in the right direction... irb(main):001:0> s = "the quick brown fox" => "the quick brown fox" irb(main):002:0> s.split.map {|w| w.capitalize}.join ' ' => "The Quick Brown Fox" Todd