From: Alex Gutteridge Date: 2007-07-10T13:02:33+09:00 Subject: Re: Simple string operation On 10 Jul 2007, at 11:54, 12 34 wrote: > seqNo = > File.basename("/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV",".*") > [-1,3].to_s > seqNo = "-" + seqNo+ ".xx.m" > puts "seqNo: #{seqNo} " # >> seqNo: -4.xx.m > > but I want it to be -004.xx.m. In other words I want the leading > zeros. > > I didn't even think the .to_s should be necessary, but tried it in > desperation. > > Thanks Your use of String#[] is wrong: -------------------------------------------------------------- String#[] Element Reference---If passed a single +Fixnum+, returns the code of the character at that position. If passed two +Fixnum+ objects, returns a substring starting at the offset given by the first, and a length given by the second. So [-1,3] gives you a string starting at the last character that is 3 characters long. You run off the end of the string before it gets to 3 characters, so it ends up just being the last character - 4. I wonder if Ruby shouldn't raise an IndexError in that case. Replace it with [-4,4] to get what you want (I think) - the last 4 characters. As you mention, the .to_s is unnecessary. seqNo = File.basename("/Volumes/MINOLTA1/DCIM/100MLT16/ PICT0004.MOV",".*")[-4,4] seqNo = "-#{seqNo}.xx.m" Alex Gutteridge Bioinformatics Center Kyoto University