From: 7stud -- Date: 2007-10-05T11:47:18+09:00 Subject: Re: opinion on a simple method Robert Klemme wrote: > On 04.10.2007 16:32, 7stud -- wrote: >>>> print word.capitalize >>> " " >>> "b " >>> "c" >>> => "a b c" > > I just wanted to point out that there might be some useless > capitalizations going on because of the white space. My solution does > not do that because it capitalizes words only. If you will, it's just > an aesthetic improvement. :-) > > I'm not sure exactly how the internals of capitalize work, but based on its output it does not blindly subtract 35 from the ascii code for the first character of a word. So, internally capitalize sorts out which characters to capitalize and which characters to leave alone(e.g. capital letters, punctuation, spaces). It is possible to use ruby to skip the useless call to capitalize for words that are spaces, like this: if word == " " print word else print word.capitalize end but it turns out that(on my system at least) using that if statement is fractionally slower than letting the C code in the capitalize method take care of words that are spaces. -- Posted via http://www.ruby-forum.com/.