From: 7stud -- Date: 2007-10-04T23:32:52+09:00 Subject: Re: opinion on a simple method Robert Klemme wrote: > 2007/10/4, 7stud -- : >> > >> >> Nice catch! Actually, your solution suffers from the same probem. :( > > I don't think so. Please look again! > >> Adding this line corrects mine, and it doesn't slow it down much: >> >> IO.foreach("data.txt") do |line| >> line.each(" ") do |word| >> next if word == " " >> print word.capitalize >> end >> end > > But it changes white spaces - which my solution does not do. > Then, I'm not sure what you were pointing out here: > >Nice idea but: > >irb(main):001:0> "a b c".each(" ") {|w| p w} >"a " >" " >"b " >"c" >=> "a b c" because my original solution and your solution result in the same output: IO.foreach("data.txt") do |line| line.each(" ") do |word| print word.capitalize end end puts "*********" File.foreach("data.txt") do |line| puts line.gsub(/\w+/) {|word| word.capitalize} end --input:--- the Bourne ultimatum harry Potter and the Order of the phoenix mission impossible --output:-- The Bourne Ultimatum Harry Potter And The Order Of The Phoenix Mission Impossible ********** The Bourne Ultimatum Harry Potter And The Order Of The Phoenix Mission Impossible -- Posted via http://www.ruby-forum.com/.