Another way:


x = "foobar" # => "foobar"
x[0,0] = "this is prepended " # => "this is prepended "
x # => "this is prepended foobar"

x variable now is "this is prepended foobar" as you can see from IRB 
output.

There are many more ways. At least one with a Regex, but it is often 
said that Regex has an overhead and hence a speed penalty, thus I think 
it is usually better to use a Regex only when needed.

There is also another way in where you re-assemble a string.

x = "foobar"

x = "abcdef " + x

But this would create a new string object.

If you use << then you do not create a new string object. (But this 
means append to... sadly there is no prepend to ... sometimes I wanted 
to use >> to mean prepend...)

-- 
Posted via http://www.ruby-forum.com/.