Project Ramon

A learning journey from a Ruby noob perspective

Regex rendezvous with Edward Anderson


In my AirPair last night with Edward Anderson, I watched him do a code review with my application. It was a wonderful critique of anything from method contents to syntax style, that answered a lot of gray areas in my understanding. I received more than one tip to increase my efficiency, and received gifts, like this Ruby style guide on github. Resources like the Ruby style guide are extremely helpful for me, as it affords me a quick and well thought out reference, allowing me more mental bandwidth to focus on my tasks in the development process.

One of the first topics Edward taught about dealt with mass assignment vulnerability, and how attr_accessible && attr_protected can be used. I also learned which controller actions (new, create & update) to pay close attention to when assigning attributes. I should also add, that new, create & update actions are just Ruby methods, they are called actions because they are created to interact with  CRUD (Create Read Update Destroy) functionality which are basic operations set to be performed in a data repository. I was curious about the differences between REST (Representational State Transfer) and CRUD, so I found this explanation:

CRUD means the basic operations to be done in a data repository. You directly handle records or data objects; apart from these operations, the records are passive entities. Typically it’s just database tables and records.

REST, on the other hand, operates on resource representations, each one identified by an URL. These are typically not data objects, but complex objects abstractions.

A standout moment on this topic, for me, was when he walked me through how someone could potentially grant themselves an Admin role within my application using mass assignment, when an unassuming new web dev like me uses an is_admin? flag to differentiate between a user with normal privileges an an admin with administrative accessability.

I am getting to regex very soon, but I wanted to point out one more method that I believe will make my debugging horrors lessen greatly. There have often been times during my short stint into programming, that I would receive an error, isolate that line of code, and still not be 100% confident of why that particular line of code was causing an exception to be raised. Edward shared with me that I could always inspect the offending variable to see its output in the browser by using raise var_name.inspect.


Finally… Some Regex!

So a regular expression is a sequence of characters to help us match or find other strings or sets of strings, using a specialized syntax held inside of a pattern.

A regular expression literal is the pattern between slashes or between arbitrary delimiters preceded by %r. Here are two resources if you would like to go further into this topic:  Tutorials Point & Ruby Documentation

Lets take a look at what Edward taught me last night to help with my zip code attribute.

Above you can see the Venue model with its attributes listed at the top, followed by an association with Event, and finally the validation code at the bottom of the file. The last validate is for the zip code and at the end of the line is the regular expression <code>/\A\d{5}(-\d+)?\z/</code>.  This just looks SO strange right? Well, that was the case for me until last night, although I still have lots to read and practice in the regex department, I’ll share what I learned about this particular regular expression literal.

First the open/close delimiters. The <code>/  /</code> slashes surrounding the regex are the boundaries of where the regex pattern is going to be added. Next is the \A\z, which means the beginning and ending of a string in Ruby.  The <code>\d{5}</code> is saying that the pattern is matching 5 decimal places (traditional partial American zip code format), while the <code>(-\d+)?</code> is allowing for the pattern to match any additional decimals after the five. We could have also done <code>(\d{4}</code> which, unless I’m mistaken takes into account the additional four decimal spots for a full American zip code format.

And there you have it! If you would like to take a peak at regular expressions sometime, why not Learn regular expressions with simple, interactive examples from RegexOne.

Here’s the full AirPair episode from my evening with Edward Anderson:


Categories: AirPair, Newbie, Ruby, Ruby on rails

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s