RegEx and Other Funny Looking Things in Ruby

- - posted in 10000 Hours, Flatiron School

I just finished my second week at the Flatiron School. This week we’ve continued to plow through Ruby. It’s fun, but kind of makes my brain hurt.

We’ve covered strings, arrays, hashes, symbols, how to create new methods, and started diving into object oriented programming.

Ruby is remarkably, syntactically clear. It isn’t quite like spoken language, but unlike many computer languages (so I hear), it isn’t complete computer gibberish. I guess this is why so many people love it. There are several things that have been making my head hurt that look nothing like normal English and don’t reveal their meaning at all by just looking at them. They are strange little arrangements of letters and punctuation marks that just look funny in the nice flow of clear Ruby expression (sorry Ruby, they look like gibberish). Apparently they are an important part of the language though, so here is my attempt to explain them.

Regular Expressions

The first time I can across a regular expression, it was in a classmates code solution to one of our assignments. At first I was taken aback how much shorter his solution was than mine, then I was like what in the blip-itty-blip is that thing? Enter regular expressions.

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern.

Some of the most important String methods that use regular expressions are sub and gsub, and their in-place variants sub! and gsub!. These methods perform a search-and-replace operation using a Regexp pattern. Note the sub & sub! replace the first occurrence of the pattern and gsub & gsub! replace all occurrences. Double note The sub and gsub return a new string, leaving the original unmodified where as sub! and gsub! modify the string on which they are called.

They can be confusing. Rubular is great tool to test what regular expressions can do. This site also contains a nice little table to guide you as you create your expression (cheat sheet—awesome).

Don’t be stupid. Use this tool wisely. A suggestion for how not to use Regular Expressions here.

Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems. — Jamie Zawinski

String Literals

Next up! String literals.

String expressions begin and end with double or single quote marks. Double-quoted string expressions are subject to backslash escape and expression substitution. Single-quoted strings are not (except for \‘ and \).

The string expressions begin with % are the special form to avoid putting too many backslashes into quoted strings. The %q/STRING/ expression is the generalized single quote. The %Q/STRING/ (or %/STRING/) expression is the generalized double quote. Any non-alphanumeric delimiter can be used in place of /, including newline. If the delimiter is an opening bracket or parenthesis, the final delimiter will be the corresponding closing bracket or parenthesis. (Embedded occurrences of the closing bracket need to be backslashed as usual.)

Backslash notation

\t tab(0x09) \n newline(0x0a) \r carriage return(0x0d) \f form feed(0x0c) \b backspace(0x08) \a bell(0x07) \e escape(0x1b) \s whitespace(0x20) \nnn character in octal value nnn \xnn character in hexadecimal value nn \cx control x \C-x control x \M-x meta x (c | 0x80) \M-\C-x meta control x \x character x itself

The string literal expression yields new string object each time it evaluated.

Splat! *

Splat? Eh?

The asterisk operator that we affectionately call the splat can have several uses.