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.


keeping myself fresh

So aside from learning new things, which I feel is essential to continuous improvement. I’ve decided to set a goal in terms of staying fresh as well. I’ll be solving one-two project euler problems a day, and learn one-two knew sign words a day. That should really only take 20-60 minutes a day and then the rest of my time I can spend learning more about MiniTest, Rails, Rails gems, and other web dev specific technologies. 


On the Project Euler side of things. Problem 3 was a doosy for me today. My original algorithm would have taken my computer over 3 years to compute the answer. An hour later I got the timing down to 22 hours :/. And finally, thanks to the realization that I only need to iterate though a range of 2 – sqrt(num) did I get algorithm down to .02 seconds

I didn’t get into app academy

So I didn’t get into app academy. I was a little bummed, but obviously that was the school for me. I know I will get into the school that best suits me. I set up a skype interview with dev bootcamp for august 27th and am waiting to hear back from hack reactor. Also I found a school in Portland that looks promising. The more I look into each of those three schools the more I can’t figure out which I like more. Before I was least pleased with hack reactor because it doesn’t really teach ruby, although, the more I think about it, I want to learn node.js and ruby in the long run, and I think they teach a lil ruby, that I’m not sure about. Either way I’m coming to realize the language is far less important the the experience and the way they teach you to learn new tech quickly. 

In the last week I learned a ton about array and string manipulation, a lil about regexp’s, and also I got much better understanding of OOP. 

Between now and aug 27th I need to pick up some extra curricular activities other than just coding all day, I’ve come to realize my coding dedication has turned me into quite the bore. Portland’s Code School wants a video of me being awesome. Recently the awesome things I do are being a dad and learn code. 

So today I decided to start teaching my daughter ASL slowly and teach myself more quickly. Today I learned the alphabet and the words “milk”, “food”, “tired”, “mom”, “dad”, “grandma”, and “grandpa”,

This learning a new actual language is really teaching me a lot about learning tech languages. You need to repeat repeat repeat. Also learn a little and use it a lot, then learn a little and use it a lot. 

So in the next few weeks I’m going to be doing Project Euler a lot and learn ASL. If you want to follow me on their, here is my friend code: 81917382509532_114d498db359e661e8976f908d59984b

Until next time fellows, keep learning, be interesting, and love our craft.

Danny Sperry

Code School Prep Work

Last you heard I had a job interview and the first of two coding challenges at app academy. 

It’s been a little over a week now, and since then I haven’t heard back from Varsity Tutors… a little bummed, not super surprised. That is really far in the back of my head though. The main thing I’ve been shooting for is getting into App Academy. They are the cheapest option and they have a very successful outline(plus you can sleep at the building if you really had to)

I did my second coding challenge. I studied the recommended readings for about a day and a half, like the first time. and decided to just jump into it since I did so exceptional last time. This time you have 4 problems to answer in an hour. So you still have the same average time limit. I couldn’t have done worse. I finished 1.5 questions in the hour and commented what I wanted to do with the rest of the second question before submitting. I have this deep sense I’m going to get into one of these immersive programs, call it intuition, God, or what ever you want. But this has given me peace with my whole situation. So I was bummed that I did Terrible(note the capital “t”), but not discouraged. I figured well, it isn’t over until they say I’m not accepted, and they haven’t said that yet, plus! I have dev bootcamp and hack reactor to apply to if app academy says nay. 

The next day I got an email from Kush letting me know that they are still interested in following up with me, they just wanted me to finish some of the same readings and homework their accepted students are required to complete. 

That curriculum is on GitHub here.

Now that that is submitted I am waiting to here back from them. But in terms of learning. That prep work really showed me a lot. I truly enjoyed learning from multiple sources in such a short period of time. You get more out of things when you get to look at it from multiple angles. 

Until next time friends. Thanks for following, or subscribing. Whatever wordpress uses 😀 

Chris Pine’s “Learn to Program” review

It’s a little over due and I’ve gotten a couple other books I’ve read since then, but I will try my best to recall my experiences with this book.


First off, I wasn’t a total noob to Ruby before starting this book. I’d read why’s poignant guide and I’d also done the work alongs. Chris Pine has a silly sense of humor that helps make light of this daunting task that all programmers go through. There is a line in my programming career that I crossed while reading this book that make me realize how programmers think. In the simplest way to explain it, breaking complex problems down to their simplest form. Chris makes you build your own .sort and .shuffle. In order to make this happen I had to look through a lot of documentation and that in itself really helped me learn some new words in the ruby language. I highly recommend this book. The author is fun to listen to, the material is well structured, and the examples are challenging enough to make you feel like a real programmer but not so challenging that you will give up. 

Side note: There will be times these examples make you want to call this whole programming thing quits. “It’s not for me, this is too hard”. Remember, programming is always hard, no matter what you experience level is. If it isn’t hard then you aren’t challenging yourself. and if you aren’t challenging yourself, then programming itself will speed right passed you. In conclusion: don’t give up and if you have no hair left on your head, step back and do something you enjoy with programming.

Learn to Program Example by Chris Pine

Here is a program I through together this morning. The only thing I haven’t gotten working is validating the month can be value.dowcase or value.upcase or value.capitalize


mo = {1 => “Jan”, 2 => “Feb”, 3 => “Mar”, 4 => “Apr”, 5 => “May”, 6 => “Jun”,

7 => “Jul”, 8 => “Aug”, 9 => “Sep”, 10 => “Oct”, 11 => “Nov”, 12 => “Dec”}

def ask string
  puts ‘What ‘ + string + ‘ were you born?’

ask “year”
year = gets.chomp
while (year.to_i > || (year.to_i < )
  puts ‘Please enter a 4 digit year between 1875 and ‘ +
  year = gets.chomp

 # need to get month working so that value, value.downcase, and value.upcase are working

ask “month”
month = gets.chomp
while (month.to_i<1) or (month.to_i > 12)
  puts ‘Please enter a month between 1 and 12’
  month = gets.chomp

ask “day”
day = gets.chomp
while (day.to_i < 1) or (day.to_i > 31)
  puts ‘Please enter a day of the month’
  day = gets.chomp

def spank (year, month, day)
  birthday = Time.local(year, month, day)
  years_old = ( – birthday) / 3600 / 24 / 365.25
  puts ‘You get ‘ + years_old.floor.to_s + ‘ spankings!!!’
  puts “SPANK!!!\n” * years_old.floor

spank year, month, day