From: Stephen White Date: 2001-07-23T16:49:57+09:00 Subject: [ruby-talk:18332] Re: capitalizing a string On Mon, 23 Jul 2001, jared l. jennings wrote: > One way that works is (assuming that string was called p) > p[0..0].upcase + p[1..p.length] I'm not finding anything much better than: p[0,1].upcase << p[1..-1] which could be defined in a function so you don't have to look at it every time you want to do this. class String def mycap str = dup str[0,1] = str[0,1].upcase! str end end Then you just use "mycap" instead of "capitalize". "fooBarPoit".mycap => "FooBarPoit" p = "fooBarPoit".mycap => "FooBarPoit" I originally wrote this as: class String def mycap str[0,1].upcase! str end end but the "[0,1]" generates an intermediate object that gets discarded after being uppercased. Whilst I understand why, it's not syntaxically obvious (to me) what is happening here. -- spwhite@chariot.net.au