Rubeque string substitution





I’ve decided to start sharing how I solve problems occasionally on this blog. I think this will be helpful for potential employers to see how I work.

The following picture is of a problem

Screen Shot 2013-08-22 at 1.57.57 PM

So we want to substitute the matched pattern of a string within the main string, for a new string. Lets start with making the pattern and actual regular expression.

pattern =

This Regexp will be used in Strings sub method(String#sub) in order to replace the matched contents of a string into the replacement parameters value,


Well that was easy, the first case passes, but the second one doesn’t. This is because sub only replaces the first matched instance it finds. Instead we’ll use String#gsub, for global sub. This will replace all instances of the matched data.


That makes the next test pass, however, we’re having issues with getting the string “??” to work like we want it to as a regular expression. This is because ? is a special character used inside regular expressions(It matches either one or none of the preceding character). is a great way to check and or just reference ruby regular expressions. So we need to escape these characters.  We can do this with Regexp.escape(pattern), this “Escapes any characters that would have special meaning in a regular expression. Returns a new escaped string” (taken from ruby-docs). So instead of .new we will use .escape right? No, if you look closely at that definition, .escape returns a string, we still need pattern to be a regexp. So we will make it work like this:

pattern =

That’s a lot better, now we only have the last test case to solve. our last test case replaces the matched data with a string that contains special substitution cases. ex: \1, \2, \3. These cases will return the exact Match data returned from the regexp. \1 return the first matched case, \2 return the second, and so on. How do we get this to just return \2. we can’t escape the use Regexp.escape(replace), that will just return silly backslashes all over the place. Instead we have to look closer at String#sub and String#gsub. “The problem is that when using sub (and gsub), without a block, ruby interprets special character sequences in the replacement parameter” this quote was taken from two-bit-fool’s answer on Stack Overflow. So when ruby sees the replacement parameter it just INTERPRETS the \1\2\3 and does some magic. We don’t want that, so lets put the replacement string into a block instead of letting it be a paramter.

self.gsub(pattern) { replace}

Now everything is passing! So this is how I solved that question. However, Lets do some recursion on this and  shorten this up a bit with regexp interpolation. With pattern we could do what we did, or we could just create a regexp and interpolate the string returned from .escape like so

pattern = /#{Regexp.escape(pattern)}/

That’s easy enough to understand, and its short enough that I think it could go on the same line, and we can skip redefining the pattern variable all together. So to sum it all up, my final answer was this:

self.gsub(/#{Regexp.escape(pattern)}/ ) {replace}  

I simplified this a little better, I skipped some steps in my head but thought this would clear up how I solved each assertion.


I finished “Learn to Program”

So as the title states. I finished “Learn to Program” by Chris Pine and I will write a view on the book some time in the near future. For now I’ll give you an idea of what kind of site I’m going to build in order to push and challenge myself.

Name: Rooster
Details: Rooster is a roster site designed for RTS, MMO, Shooters, and really any game that has a group of players that get together and associate themselves under a common alias. The name isn’t concrete, but the idea is.

  • The site will have a home page with a sign in/up and a navigation that will have a list of game types(MMO, RTS, etc.) once clicked you will see a list of games within that type
  • once a specific game is clicked all clans within that game will show. On all the types, games, and clans you will be able to view a forum.
  • Within each clan page you will be able to see the roster.
  • If you are a member of a clan page you will be able to edit your profile, view the roster, view a home page feed, and clan rules and responsibilities.
  • As an admin you will be able to edit what fields will be displayed within your roster, edit the fields within your roster, edit individual profile pages, post new feeds on main clan page, edit or delete old ones. edit rules and responsibility page.

I’m sure this is an odd way of explaining the use of my site. I hope you get the point. If you don’t get the point, you will most certainly understand it after I’ve completed the site. Check out my progress at github.

Thanks to those of you that have viewed my blog and started following me. If anyone has any questions for me I’m always happy to try to help. I’m no expert but I firmly believe that a person cannot improve his/her skills without crossing that line of comfort.

From Rails to Ruby

So I got through the first 2 sections of ‘Agile Web Development with Rails’, you can see my progress on Github at I started reading ‘Learn to Program’ by Chris Pine. A couple years back I got this book and went through the first 90 or so pages in a day or two, then I got to a point where they want you to build your own sorting method for arrays as opposed to using the simple array.sort

View my code at

It took me a little bit to find it, I searched all through and couldn’t find anything that would let me find the least value’d or most valued object inside an array. I searched just that and found the .min and .max feature which just made it all come together so easily after that.

This ruby book is relatively easy, the examples just take a problem solving mind that can wrap around the issue and a little bit of research through the documentation. I’m sure that .min is somewhere in the documentation, I just didn’t see it there. Last time I read ‘Learn to Program’ I got stuck and gave up. This time around I’m pushing through all things that seem difficult. No rails, ruby, javascript, jquery, or git are going to conquer me, maybe for a little bit, but in the end I will figure it out.