Q:

Add square bracket to the first character of string

I need to put the first character within square brackets. For example, I want to convert "abc" to "[a]bc". How do I do this in ruby using regex?

A:

With regex:
"abc".sub(/(.)/, '[\1]')

Without regex:
s = "abc"
s[0] = "[#{s[0]}]"

