----- Original Message ----- 
From: "Daniel Bretoi" <lists / debonair.net>
To: "ruby-talk ML" <ruby-talk / ruby-lang.org>
Sent: Sunday, November 03, 2002 1:18 AM
Subject: substitution problem


> Hi All,
> 
> I'd like to capitalize every word in a string.

One way:

  str.split.map {|x| x.capitalize}.join(" ")

but you may not be happy with the way it treats
spaces and such.
 
> I was hoping this expression would do it:
> 
> newstr = str.gsub(/\b(\s)?([a-z])/,'\1\2'.upcase)
> 
> but it turns out that as \2 is not evaluated right away, the .upcase has
> no effect. Suggestions?

Maybe break it up.

  str = "what hath god wrought?"
  newstr = ""
  str.scan(/(\w)(\w+)(\W)*/) {|x,y,z| newstr << x.upcase << y << z}
  p newstr  # "What Hath God Wrought?"

This works for me. Depends on your data.

Hal