Hi --

On Fri, 14 Mar 2008, Dan Diebolt wrote:

> Don't use a regexp. It isn't clear what you want to happen if there isn't a middle character if the string has a odd integer for its length. Try using some type of substring approach such as this:
>
> str = "xxx"
> str[0..str.length/2-1] +
> str[str.length/2,1] * 4 +
> str[str.length/2+1..str.length]
> => "xxxxxx"

A slightly different way to do (I think) the same thing:

irb(main):036:0> str = "xxx"
=> "xxx"
irb(main):037:0> str[str.size/2] = str[str.size/2].chr * 4
=> "xxxx"
irb(main):038:0> str
=> "xxxxxx"

I was hoping for a *= operation but it doesn't quite fit :-)


David

-- 
Upcoming Rails training from David A. Black and Ruby Power and Light:
   ADVANCING WITH RAILS, April 14-17 2008, New York City
   CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!