Title photo
frugal technology, simple living and guerrilla large-appliance repair
Fri, 12 Sep 2014

Ruby's 'insert' method is something I'm definitely going to use

It's not a secret that I'm starting to look into the Ruby programming language. I've got a mess of second-hand books, plus there are plenty of helpful web sites.

I was looking into the gsub method of search/replacing in Ruby when I stumbled across something very useful: the insert method.

I'm sure there are plenty of better ways to do this, but the fact that I can do this and understand it ... that's something.

Here is what I'm talking about. I did it all in the interactive Ruby shell (aka irb) and have revised it because it's even easier to type out than I thought:

irb(main):017:0> phrase = "The quick brown fox jumped over the lazy dog's back"
=> "The quick brown fox jumped over the lazy dog's back"

irb(main):020:0> phrase.insert 0, "<bold>"
=> "<bold>The quick brown fox jumped over the lazy dog's back"

irb(main):024:0> phrase.insert -1, "</bold>"
=> "<bold>The quick brown fox jumped over the lazy dog's back</bold>"