Pushing the boundaries of the webbernets.

Fun With Gsub

During a recent ruby meetup we touched on some finer points of ruby’s global substitution method. That’s right gsub, likely you have used it in your every day ruby code and never knew all the things it could do. The most common usage of gsub is for simple pattern match and replace.

"some string".gsub(/some/, "some other")
# "some other string"

However this is just the tip of the iceberg of what you can do with gsub. So lets explore that a bit and see what all we can do with gsub.

Another way to use gsub is with a ruby block.

"some string some string".gsub(/some/) do |match|
# "SOME string SOME string"

The block allows you the chance to do more complex computation of each match as it’s passed into the block, allowing you greater control than the first option.

But there is a more interesting option for gsub that I only recently became aware of. gsub can also take a hash of key value matches.

"some string".gsub(/(string|some)/,{"some" => "some other", "string" => "thing"})
# "some other thing"

This can be used to handle multiple matches in the same way each time. While this is the most specialized use case for gsub it can be handy to pull it out when the right time arises. Such as a 1337 Sp34k traslator.

def translate_to_leet_speak(string)
  string.gsub(/[aetlo]/, 'l' => '1', 'e' => '3', 't' => '7', 'o' => '0', 'a'=> '4')

# 1337
# h4x0r
# sp34k

Granted this is not the best translator, since we are not handling the edge cases of transforming words to their leet speak equivalent, for instance “hacker” should be translated to “h4x0r” in such a case. In the above example we had to pass through haxor instead of just hacker. However we could easily use gsub again to translate know words into their alternate form, so “elite” would become “leet” and “hacker” would become “haxor” then run those through the original gsub.

def translate_to_leet_speak(string)
  string.gsub!(/hacker|elite/, 'elite' => 'leet', 'hacker' => 'haxor')
  string.gsub(/[aetlo]/, 'l' => '1', 'e' => '3', 't' => '7', 'o' => '0', 'a'=> '4')

Notice the bang “!” on the end of gsub in the first gsub call. When calling gsub with and exclamation mark or “bang” gsub will then modify the original string it was called on.

Using the above method we can now catch known words that need to be changed to an alternative and unknown words that contain the key letters would still be handled on a basic level. This is essentially how many translation programs handle translation, they take a matrix of words that map to other words or series of words and then proceed to find and replace them. Although this doesn’t yield the best results, and completely ignores grammar and other considerations for true translation.

Happy hacking with gsub.

Getting Started With Phoenix Framework

Install Elixir

In order to get started with phoenix you will need to install elixir. Fortunately installing elixir is pretty painless. The elixir installation page has some really great documentation on how to install for many different environments. My preferred method is with homebrew, if you aren’t already using it you should be.

With homebrew the install is just running the following command.

brew install elixir

Install Pheonix

Now that we have elixir installed we need to install a package management utility called hex. This is required for dependency management in phoenix.

To install Hex run this command.

mix local.hex
  • Note: mix is a elixir task runner command. Very similar to rake in ruby.

Now lets actually install the phoenix package.

mix archive.install

Awesome! Phoenix is installed!

Starting your Phoenix Project

Let’s create a new phoenix project.

mix pheonix_app

This command will create a new phoenix application in the folder called phoenix app. So lets move into that directory.

cd pheonix_app/

Now that we are in our Phoenix app’s directory. We need to run a few commands to launch the most basic phoenix page.

We need to ensure all of our dependencies have been downloaded and are available to us.

mix deps.get

this command is equivalent to “bundle install” for ruby applications. If you are not familiar with ruby then just think of this command as a way to ensure all the required parts of your app are there. During the course of building a phoenix application you will use this command many times to download and include libraries which will ease your building process.

Once we have all of our dependencies downloaded and in our app we need to ensure we have a database to connect to. Now this is not required for some apps, but it is usually required on most applications of some substance.

Before we can configure the database we need to ensure we have the correct credentials to access the database installation and interact with it. In phoenix these credentials are stored in the respective environment files for development this is config/dev.exs for production the file would be config/prod.exs

config :phoenix_app, PhoenixApp.Repo,
     adapter: Ecto.Adapters.Postgres,
     username: "postgres",
     password: "postgres",
     database: "phoenix_app_dev",
     hostname: "localhost",
     pool_size: 10

Change the user name and password to your local database’s username and password

Now we can create the database.

mix ecto.create

Once you have the database we can start the server.

mix phoenix.server

Now you can load http://localhost:4000 in your browser and you should see the phoenix welcome page.

An Image of the phoenix framework getting started page

Remove Turbolinks From Rails 4

If you don’t want to use Turbolinks with your Rails 4 app.

Remove turbolinks gem from your Gem file.

remove the following line

gem 'turbolinks'

After you remove that line run a bundle install to update your gem file and dependencies.

Remove turbolinks javascript includes

In your application.js file which should be located here app/assets/javascripts/application.js

remove the following line.

//= require turbolinks

Remove trubolinks key value pairs from application layout.

Inside you application layout, app/views/layouts/application.html.erb.

Find the following two lines.

 <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
 <%= javascript_include_tag "application", "data-turbolinks-track" => true %>

Remove the “data-turbolinks-track” key value pair. Once you are done your stylesheet tag and javascript tag should look like this.

  <%= stylesheet_link_tag "application", media: "all" %>
  <%= javascript_include_tag "application" %>

RailsConf 2015

My trip started off like any other cross country trip I’ve taken: I packed my bags and headed for the airport. I was super excited for the upcoming conference and my very first RailsConf. I’ve been to many software engineering conferences, but none with a ruby and rails focus. The ruby community is by far one of the most welcoming I’ve experienced, and I was confident I would meet tons of great people and make friends.

After a two-hour delay in Houston, TX, I finally landed in Atlanta, GA and jet to my hotel room in the Ellis hotel. I dropped off my bags and made my way down the block to the Westin Hotel where the guide scholar meet-and-greet was being held.

Walking down the street, I recognized a face in the crowd and racked my brain to think of where I knew her from. Then it hit me! It was Coralline Eda Ehmke, from the Ruby Rogues podcast walking down the street toward my hotel. As the name popped into my head, she had already passed me, and I wheeled around to shout “Coraline!” She had this look on her face that said “Who are you and how do you know me?” I told her I’m a big fan of the Ruby Rogues and love her as a regular panelist. We chatted for a couple short minutes, and then I continued on my way to the scholar/guide gathering.

I finally made it to the last thirty-minutes of the two-hour gathering. I meet my Scholar, Michelle Bonat, and we started getting to know each other. We talk about our day jobs and what interests us, what talks we’re looking forward to, what we want to gain from the next three days at RailsConf. We were excited to meet people and network, and the talks were a bonus, an icing on the cake to the greater value of the community.

The next day we met in the large hall where DHH (David Heinemeier Hansson, the creator of Rails) would give the opening keynote. There was quite a bit of buzz about what new features would be included in the next and fifth iteration of Rails. After a brief intro, DHH took the stage, speaking extensively about “Integrated Systems” a rebranding of “Rails Monolith”. Many of the people I spoke to during the conference felt this was his way of saying “SOA is dead.” But this time, he managed to stay a bit more diplomatic and refrained from another “TDD is dead” incident like last year’s keynote. He wrapped up the keynote with the introduction of Turbolinks 3 the next iteration of the infamous Turbolinks. Turbolinks is a javascript framework that is built into a gem and comes default with any new rails app. When a user clicks on a link, Turbolinks takes over and updates the body of the page and only the body tag with new html. It then updates the history for the browser so the back button will work as expected. This has caused quite a number of issues due to how jquery works, and fires events based on page loading. While this new iteration of Turbolinks was expected, what was unexpected is the new use case for Turbolinks, and a websocket implementation called ActionCable. With the addition of ActionCable, you will be able to have parts of the page that are turbolinked and other parts of the page that can remain static and cached. I was super excited to hear this! Finally, Turbolinks might actually be useful in some of my projects.

After DHH’s awesome keynote, we had a 15-minute break before the first talk began. I talked with my scholar and helped her to pick talks that she would find both interesting and useful, since this was her first conference.

As the conference kicked off, I was well aware of the hallway track. It was as entertaining and interesting as any of the talks. The “hallway track” is the conversation people have in the hallways in between talks. Normally at most conferences, these are just conference goers having discussions about the talks they just saw. However, I realized at RailsConf this was where you could also meet and chat with some of the most influential rails celebrities. On the first day, I saw that someone had managed to pull DHH to the side and start a conversation. Soon, ten of us were asking him questions. It shows how powerful a conference can be in bringing people together and involving any member of the community with even the most iconic.

During lunch on the first day, we had a CodeNewbie get together. This was by far one of my favorite parts of the conference. The CodeNewbie community is awesome and so friendly. I brought my scholar with me to the CodeNewbie lunch and her first experience was an awesome one. While short and sweet, this was an amazing chance to meet and get to know some of my fellow CodeNewbies.

The first day was closed out with Sara Chipps, an incredible Javascript developer who started the company Jewelbots. She gave an excellent presentation on how they work and why the company was started. It was great to think some young girls maybe inspired to pick up programming because of jewelry. While Sara and her innovation were inspiring, the show was stolen by three high school students who attend the Flatiron School and only recently started learning to code. They showed off two drones that danced in synchronization. It was an awesome show of just how quickly your programming skills can be used to interact and create something cool, or maybe just delight a few hundred people at a conference.

The second day kicked off with Aaron Paterson (A.K.A. TenderLove) giving the keynote, and the trolling commenced. He trolled Kent Beck and DHH within the first 10 minute. In fact, one of the main themes of the keynote was trolling Kent Beck. As much as I love watching Tenderlove troll people, the keynote did have some really great points on performance and the work he’s been doing with bundler to make it more performant.

We proceeded to the expo area where many of the most well known ruby on rails tools and integration vendors were present and passing out swag. Needless to say, this was pure anarchy. The expo area was an awesome chance to interact and talk with some of the developers and support teams for products like Heroku, Mandrill, RubyMine, Skylight, and many more.

After the pure bedlam of the expo area, we continued to our talks for the second day. The talks were all choice quality, but one in particular stuck out in my mind as possibly the best conference talk I have ever seen. Adam Cuppy of ZEAL gave a talk called “What If Shakespeare Wrote Ruby?” I am not sure this talk fits into the category of talk, as much as it does “performance and spectacle”. This talk was not only entertaining, but had a great message for software engineers everywhere. If you ever have a chance to see Adam Cuppy speak, I wouldn’t miss it.

The second day was closed out with happy hour and lightning talks. Happy hour was definitely happy. Everyone grabbed appetizers and hung out. The appetizers were quickly demolished, and the din of the crowd was deafening. It was amazing to see so many people sharing their love for Ruby and Rails. The lightning talks were hit or miss, but some were fantastic. CodeNewbies had a wonderful lightning talk given about the community and how awesome it is. Then the after parties started. HIRED was celebrating the opening of their Atlanta office and rented out a bar at the top of the Hilton. Engine Yard rented out JoyStick Game bar, an arcade with booze, and yes it was as epic as that sounds.

That meant that when the third and final day of the conference started, a good majority of the crowd was hungover The Ruby Hero Awards were presented: Nobuyoshi Nakada, Eileen Uchitelle, Sarah Mei, Zachary Scott, Jeremy Evans, Sam Saffron. Thank you for all your contributions to the Ruby community.

We moved on to a panel with members of the Rails core team. These are people who work on the core and are responsible for maintaining and building new features in ruby on rails.

After the panel, we made our way to the last remaining talks for the conference, and wrapped up the day with a wonderful talk from the awesome Kent Beck. He gave a keynote on how he feels at work and how he strives to be at ease in his job. As a young software engineer, he was taught to do a good enough job that any flaws would be someone else’s fault. This passing of the blame, as well as many other common practices, caused him to be very uneasy at work, so Kent developed some rules for how he creates a sense of ease for himself, and alleviates work-related worries.

And then, the conference was over. We said our goodbyes and many of us ran to catch a flight home. RailsConf was an onslaught of learning, meeting people, and making friends, I loved every minute of it, and left the conference thoroughly exhausted. I headed for home, my life enriched just a little more by the experience.

If you happen to make it to RailsConf next year, don’t be a stranger – come and say hi :)