2007/10/4, Alex Gutteridge <alexg / kuicr.kyoto-u.ac.jp>:
>
> On 4 Oct 2007, at 13:00, Lloyd Linklater wrote:
>
> > I am trying to find the better way to do things ruby style.  I
> > needed to
> > make a method that would read in a file of movie titles and capitalize
> > each word.
> >
> > "this is a string".capitalize just gets the first word, so I did this:
> >
> > File.open('\movies.txt') do |f|
> >   while line = f.gets
> >     s = ""
> >     line.split(/ /).each {|one_word| s += one_word.capitalize + ' '}
> >     puts s.chop
> >   end
> > end
> >
> > Is there a cleaner or more "rubyish" way to do this?
> > --
> > Posted via http://www.ruby-forum.com/.
> >
>
> How about:
>
> File.foreach('\movies.txt') do |l|
>    puts l.split.map{|w| w.capitalize}.join(" ")
> end

I'd probably do this:

File.foreach("movies.txt") do |line|
  puts line.gsub(/\w+/) {|word| word.capitalize}
end

Kind regards

robert