Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this do?

gsub(/^/, "\t" * num)

What character is being substituted?

like image 348
David Avatar asked Dec 17 '22 14:12

David


1 Answers

No character is being substituted, it is just inserting num tabs at the beginning so you could say that it is substituting the zero width "beginning of line" marker. Whoever wrote that would have been better off with something more like this:

tabbed = "\t" * num + original

A regular expression really isn't the right tool for simple string concatenation.

Clarification: If you're expecting your string to contain multiple lines then using:

gsub(/^/, "\t" * num)

to prefix all the lines with tabs is a reasonably thing to do and less noisy than splitting, prefixing, and re-joining. If you're only expecting to deal with a single line in your string then simple string concatenation would be the better choice.

like image 91
mu is too short Avatar answered Jan 02 '23 13:01

mu is too short