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. ...

February 26, 2021

Binary to Decimal Conversion in Ruby

This week I wrote an algorithm in Ruby to convert binary numbers into decimal numbers. Here’s the problem description, from Exercism: “Convert a binary number, represented as a string (e.g. ‘101010’), to its decimal equivalent using first principles. Implement binary to decimal conversion. Given a binary input string, your program should produce a decimal output. The program should handle invalid inputs.” ...

May 4, 2017

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). ...

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. ...

June 28, 2016

Don’t miss my next essay

Hear from me immediately when I post: no ads, unsubscribe anytime.