`group_by` with default

Here’s some code that made me do a double take. something = items[nil] It’s that [nil]. What’s going on here? It turns out that items is the return of a call to Ruby’s group_by. group_by groups by whatever the block evaluates to, including nil. [ { shipping: 'fedex', id: 1 }, { shipping: 'fedex', id: 2 }, { id: 3 } ].group_by { |item| item[:shipping] } => { 'fedex' => [{ shipping: 'fedex', id: 1 }, { shipping: 'fedex', id: 2 }], nil => [{ id: 3 }] } Hence the key nil. ...

November 5, 2021

Edit in Pry

Here’s a scenario: you’re hacking on Ruby code in pry, and it’s a mess. Blocks chained on blocks, variables named a and other_one, assignments to _. You can understand it, but nobody else can, and one extra } breaks everything. The solution is the edit command. Typing edit in pry loads the current buffer into your editor of choice. Jump in there and turn those inline blocks into do/end, assign some variables, write a method: iterate on some quality code in the comfortable, predictable setting of your text editor.

August 3, 2021

Hash Equality

Ruby’s ‘principle of least surprise’ strikes me again. From the docs: Equality— Two hashes are equal if they each contain the same number of keys and if each key-value pair is equal to (according to Object#==) the corresponding elements in the other hash. An example: hash = { 7 => 35, "c" => 2, "a" => 1 } doppelganger = { "a" => 1, "c" => 2, 7 => 35 } hash == doppelganger #=> true

May 5, 2021

Show Proc Source Location

Today I was investigating a Ruby Proc created in a gem, and I wanted to know how it was defined. Other than reading source code, there doesn’t seem to be a good way to do this! Prove me wrong. We can get close by using Ruby’s #source_location function on Proc: some_proc.source_location => ["/gems/2.4.0/bundler/gems/spree/core/app/models/spree/order.rb", 34] This command shows us the line where Proc is defined. Go there to continue source-diving.

April 28, 2021

Steps Within Steps

Cucumber step definitions are useful for explaining in detail the actions a user might take on your site. But after a while, they become repetitive. Certain sequences of steps, like a user signing in, happen frequently in a robust test suite. We should not have to repeat them all every time we build a feature. One solution is calling several steps from within another step: Given 'I am a signed in developer' do steps %Q{ Given I am a developer with credentials And I see the homepage When I click sign in Then I see the signin page When I enter my credentials And I click the sign in button Then I see my username in the upper right } end When you assert the topmost Given, all steps in the block will be interpolated and called. You still get the confidence of a rigorous test, but with a more concise declaration.

April 21, 2021

Upgrade Rubygems

To update rubygems: gem install rubygems-update update_rubygems gem update --system

April 21, 2021

Reset a Gem

If you’ve been mucking around in your Ruby dependencies, bundle pristine is your friend; it resets all of the gems in your Gemfile to their pristine condition. This can be a slow on a big project. Target the specific gems you worked on today with: $ bundle pristine <gemname>

April 20, 2021

Augmenting an Object With the Proxy Pattern

Let’s look at the proxy pattern in Ruby. Here’s a Ruby class that claps: class Hands def clap "👏" end end And here’s me using an instance of it to do something surprising. >> 3.times { hands.clap } "👏" "👏" "👏" => 3 >> hands.times_called(:clap) => 3 times_called is not defined in Hands, or any parent of it. So what gives? Did I leave it off the first code block? What’s going on here? I recently learned about and had the opportunity to implement a proxy class. In this post, I’ll share what I learned, showing how hands gets this special behavior. ...

February 26, 2021

Porting TIL from Rails to Phoenix: Initial Commits

Last week, I started a new project: porting Today I Learned from Ruby on Rails to Phoenix (Elixir). The first few commits were pair programmed with Josh Branchaud, and I hope to work with him and other Rocketeers going forward. The repo for the new codebase is here. There’s a lot of work to do to reproduce even the MVP functionality of TIL. We’re using the ‘Projects’ feature of Github to track progress right in the repo. ...

November 29, 2016

Server Side Sorting in Ruby

Recently a friend asked me this question about server-side sorting in Rails. Here’s the question and my answer. The question: I’m working on an app that displays movies. The index page simply lists all movies in a table %table#movies in HAML. There is a clickable ‘Movie Title’ header above the list of movies and when clicked, the page should reload but in alphabetical order. Therefore I assume I need to use .order(:title) somewhere in the app. I was able to simply order all movies when the page loads by doing the following in movies_controller.rb: ...

June 28, 2016