TIL

After being inspired by a bunch of Ruby Rogues episodes and in part by my colleague Brett’s year of Ruby tips I decided to try a simple experiment in which I get into the habit of starting to document every time I encounter some new piece of information. Sometimes days pass by when I feel like I haven’t grown in any appreciable way, and this is a really great and deliberate reminder that you learn something new every day, and you learn something new and remember to make a note of it almost every day.

Being the hardcore Evernote user that I am, I started dumping these notes into a new tag called things I learned today.

It’s a great habit. I find I remember these things a little better, and even when I don’t, it doesn’t matter! I have it saved safely in Evernote! I don’t just use this for programming topics, either. I make a note any time I figure out something and think “hey, that’s neat!” or if I was trying to figure out something and it was hard to google because I had such a tenuous grasp on how to describe it (like songs where the tune is stuck in my head but I can’t remember anything about the lyrics).

Here are a few things that I learned over the past several weeks:

Refinements in irb

It turns out that due to a change after refinements were introduced in Ruby 2.0.0, they’re not nearly as easy to use and play with in an irb console. You’re better off writing them in a file and including the file in IRB if you want to play. Learn more

Markdown to Evernote

There exists a handy OS X service for taking some Markdown and sending it into an Evernote note. I love this because I love to type bulleted lists using Markdown’s bulleted list syntax, but just having plain old Markdown in my Evernote note doesn’t feel nearly as fancy. The maintainer admits he has been neglecting it a bit, but you can check it out on his blog.

Regex Postgres queries with ActiveRecord

Here’s how to construct an ActiveRecord query to find records where a field matches a regex pattern:

things_with_a_pathetic_90s_description = Thing.where("description ~ '^.*NOT!$'")

A photographic diagram of how various ingredients affect the resulting cookie. Delicious…

Rendering collections without loops

Using ERB templates in Rails, there’s a concise way you can render a partial of every item in a collection:

<%= render partial:"line_item", collection:@line_items, as: :line_item %>

Enumerable#group_by

I love SQL’s GROUP BY. I wanted to do something similar with a plain old Ruby array, and in the process I learned why ActiveRecord calls it group and not group_by; the method name was taken. Also, that was the best I could do at coming up with a good mnemonic device for remembering which is for which.

(1..6).group_by { |i| i%3 } #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}

Leave a Reply

Your email address will not be published. Required fields are marked *