This Week I Learned: 2020 W21

Keyboard shortcuts and Ruby ramblings

Published May 24, 2020

MacOS quick search in finder

In MacOS, I’m bad at remembering which of Control, Option, and Shift is the correct bonus modifier for the action I’m trying to do. While trying to hit Ctrl+Cmd+Space to get the emoji keyboard I learned about last week, I sometimes accidentally hit Option instead of Control. That happens to bring up a Finder window with the search field automatically focused.

I used that intentionally for the first time this week! I was trying to find a download that I might have moved into a project directory, but was probably still in ~/Downloads.

I had to search a bit to find docs for this, but it’s considered part of the Spotlight shortcuts.

Ruby private_constant

My first programming language was Python, so I “grew up” in an environment without private class variables or methods. The underscore prefix conventions1 were useful and effective, but they could always be circumvented if you were willing to take the API compatibility risk.

Ruby does have language-enforced access levels. Like everything else in Ruby, they can be circumvented with the right incantation. But sometimes they circumvent themselves! Here’s an example that broke my intuition:

class C

  VISIBLE = 'visible'.freeze

  def uncallable

Trying to call raises the NoMethodError I expect, since that method is private. But C::VISIBLE, even though it’s defined after the private modifier, is still public!

I can’t find an official citation for it (does Ruby even have a spec?), but this would be consistent with private and protected modifiers being applied only to methods.

The “right answer” is to use private_constant:

class C
  INVISIBLE = 'invisible'.freeze
  private_constant :INVISIBLE

Now, C::INVISIBLE raises a NameError.

If you think this is the kind of thing that Rubocop should warn about (I do!), that’s already in progress. That’s actually how I found out about this: Mangesh (the author) shared this in our Gusto engineering chat.

Speaking of things I learned from work Slack (thanks Jeff Carbonella and #protips!), here’s another useful shortcut. When browsing a source tree in GitHub, you can press y to navigate to the permalink for whatever you’re looking at.

I do this all the time, and now I can do it faster! Here’s the full list of related keys.

How to deploy a Rails app

I spent my personal project time this week getting a Rails app deployment pipeline set up for myself. The project was originally written as Go mini-services because that’s what I was doing that at MemSQL at the time. At Gusto, I’m in a Rails monolith, so I’m rewriting the app to gain experience with work-related tools. Plus, using a very popular web framework gives me access to more convenient libraries and examples.

I’ve tried to deploy to Heroku before, but the convenience of “git push to deploy” costs more money and trust than I’d like. In particular, some of the benefits I wanted from Ruby and Rails (Devise for more robust authentication, Sidekiq for background jobs) require paying for add-ons or scaling to more dynos. I’m not ready to spend $50/month for this side project just yet.

Using Ansible and Capistrano (I learned both specifically for this purpose), I now have a dev server running a trivial Rails app! I don’t have to be worried about misconfiguring things while I’m learning, because I can throw the whole server away and start over if I mess it up too badly.

I had to piece together information from multiple online sources, so I’ll go into the setup in a dedicated post (someday).

  1. Single underscore means “please don’t use this”, as in _convenience_util. Double underscore (“dunder”) introduces unrelated name mangling that happens to make it more difficult to call (__internal_only => _ClassName__internal_only). ↩︎