Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

28 posts by mattpolito @mattpolito

Return difference between Lambda and Proc in Ruby

More differences between Procs & Lambdas

If a Proc has an explicit return then that return bubbles up to where it is being used.

Let’s take a look at a Proc’s behavior:

# Explicit return
def work_it
  p = Proc.new { puts "Workout"; return }
  puts "Pre workout"
  p.call
  puts "Post workout"
end

=> work_it
Pre workout
Workout
=> nil
# No explicit return
def work_it
  p = Proc.new { puts "Workout" }
  puts "Pre workout"
  p.call
  puts "Post workout"
end

=> work_it
Pre workout
Workout
Post workout
=> nil

Now let’s look at lambdas:

# Explicit return
def work_it
  l = -> { puts "Workout"; return }
  puts "Pre workout"
  l.call
  puts "Post workout"
end

=> work_it
Pre workout
Workout
Post workout
=> nil

Now even with the explicit return in the lambda, the method is able to complete its own logic path.

Arity difference between Lambda and Proc in Ruby

Many devs think Procs & Lambas in Ruby are interchangable… and it a lot of cases they can be.

However I did come across a difference to be aware of.

Procs do not enforce arity where a Lambda will.

Let’s take a look at a Proc’s behavior:

# Argument provided
p = Proc.new { |arg| puts arg }
p.call("TEST")
TEST
=> nil
# No argument provided
p = Proc.new { |arg| puts arg }
p.call

=> nil

Now let’s look at lambdas:

# Argument provided
l = ->(arg) { puts arg }
l.call("TEST")
TEST
=> nil
# No argument provided
l = ->(arg) { puts arg }
l.call

=> wrong number of arguments (given 0, expected 1) (ArgumentError)

See how there is strict arity on a lambda where the proc will not complain.

Comparison Validate more than Numbers in Rails

As of Rails 7 we can now use comparisions in validations for more than just numbers!

Previously we could do this but only against an integer

class Procedure
  validates :appointment_count, numericality: { less_than: 5 }
end

Now we can compare against many other types. Here is a quick example validating that an end date is further in the future than a start date.

class Procedure
  validates :start_date, comparison: { greater_than: ->(_) { Date.current }
  validates :end_date, comparison: { greater_than: :start_date }
end

Cool thing is you can pass a Proc or Symbol. A symbol can represent a method on the class. The Proc can represent anything. Its argument is the instance of the class.

For more info, check out this PR.

Always declare columns for SQL query in Rails

When ignoring a column in an ActiveRecord query you’ll receive a query that declares the column names of the table explicitly versus using *.

What if you do not want to ignore a column to get this functionality? Rails 7 will introduce an ActiveRecord class attribute to do just this.

class Procedure < ApplicationRecord
  self.enumerate_columns_in_select_statements = true
end
Procedure.all

=> SELECT "procedures"."id", "procedures"."name", "procedures"."created_at", "procedures"."updated_at" FROM "procedures"

When enumerate_columns_in_select_statements is set to true, ActiveRecord SELECT queries will always include column names explicitly over using a wildcard. This change was introduced to provide consistency in query generation and avoid prepared statment issues.

Note: it can be declared at the app configuration level as well.

module MyApp
  class Application < Rails::Application
    config.active_record.enumerate_columns_in_select_statements = true
  end
end

With that change, all ActiveRecord queries will avoid the wildcard.

Ignore columns on ActiveRecord queries in Rails

ActiveRecord models have ignored_columns defined as a class attribute. This allows us to, just like it says, ignore a particular column from our queries and returning ActiveRecords.

class Procedure < ApplicationRecord
    self.ignored_columns = [:created_at, :updated_at]
end

If we perform a lookup, this is what we’ll now see.

Procedure.all

=> SELECT "procedures"."id", "procedures"."name" FROM "procedures"

Notice the subtle difference?

Without the ignored_columns declaration, we’d see a query like this:

Procedure.all

=> SELECT "procedures".* FROM "procedures"

Using NULLS FIRST / NULLS LAST ordering in Rails

Methods nulls_first & nulls_last were added to Arel in Rails 6.1 for PostgreSQL and in most other databases in Rails 7.

In an earlier TIL, I showed how to order using Arel.

Procedure.order(Procedure.arel_table[:name].desc.nulls_first)

=> SELECT "procedures".* FROM "procedures" ORDER BY "procedures"."name" DESC NULLS FIRST
Procedure.order(Procedure.arel_table[:name].desc.nulls_last)

=> SELECT "procedures".* FROM "procedures" ORDER BY "procedures"."name" DESC NULLS LAST

Here the desc method is wrapping the Arel attribute in a node so that it can be utilized in order. nulls_first/nulls_last is just wrapping the previous ordering node.

Procedure.arel_table[:name].desc.nulls_first

=> 
#<Arel::Nodes::NullsFirst:0x00007fb73ebf18f8
 @expr=
  #<Arel::Nodes::Descending:0x00007fb73ebf1920
   @expr=
    #<struct Arel::Attributes::Attribute
     relation=
      #<Arel::Table:0x00007fb735a02dc0
       @klass=Procedure(id: integer, name: string, created_at: datetime, updated_at: datetime),
       @name="procedures",
       @table_alias=nil,
       @type_caster=
        #<ActiveRecord::TypeCaster::Map:0x00007fb735a02cd0
         @klass=Procedure(id: integer, name: string, created_at: datetime, updated_at: datetime)>>,
     name="name">>>

I mention this as you need to define the order before utilizing the nulls methods.

Using Arel in ActiveRecord ORDER Queries

I utilize Arel in ActiveRecord where all the time but it never occured to me that order can also take an Arel node.

Procedure.order(Procedure.arel_table[:name].desc)

=> SELECT "procedures".* FROM "procedures" ORDER BY "procedures"."name" DESC

For such a trivial example I’d say that it’s much clearer to just use ActiveRecord in this case.

Procedure.order(name: :desc)

=> SELECT "procedures".* FROM "procedures" ORDER BY "procedures"."name" DESC

Cool piece of knowledge for possible use in a more complex ordering case.

Custom URL helpers in Rails

Ever want a named route for something that’s not necessarily a resource in your Rails app?

In your any of your route files you can utilize direct.

# config/routes.rb

Rails.application.routes.draw do
  get 'foo', to: 'foo#bar'

  direct :get_to_the_goog do
    "https://google.com"
  end
end

This gives a nice named route of get_to_the_goog_url!

This can be even more useful as the return value of the block has to be valid arguments that you would pass to url_for. So you can use pretty much anything you’d normally use to build a url helper.

Let’s modify this just a bit to be more useful.

# config/routes.rb

Rails.application.routes.draw do
  get 'foo', to: 'foo#bar'

  direct :get_to_the_goog, search: nil do |options|
    "https://google.com/search?q=#{options[:search]}"
  end
end

Now we can call get_to_the_goog_url(search: "TIL").

Pretty cool… just take note of a few caveats. You get access to the _path version of the named helper but even if the helper is using a hard coded string (like our example above), it will remove the domain info (rightfully so). Also you are not able to use the direct functionality inside of a namespace or scope, however it will raise an error if you do… so that’s nice.

Break up large routing files in Rails

Breaking apart a routing file could be useful if you have large a different concepts in your app with their own larger route sets.

To do this you can utilize draw

# config/routes.rb

Rails.application.routes.draw do
  get 'foo', to: 'foo#bar'

  draw(:admin)
end

This will try to load another route file at config/routes/admin.rb. That file is just another normal routing file.

# config/routes/admin.rb

namespace :admin do
  resources :users
end

Subtlety between Rails' #try & Ruby's #&.

Many would say that the safe navigation operator (&.) in Ruby is a drop in replacment for Rails’ #try. However I came across an interesting case where they are not the same.

thing = nil
thing&.something
> nil
thing = nil
thing.try(:something)
> nil

Seems pretty much the same right?

However what happens when the object receiving the message is not nil but does not respond to the message?

thing = Thing.new
thing&.something
> NoMethodError: undefined method 'something' for <Thing:0x0000>
thing = Thing.new
thing.try(:something)
> nil

Hopefully not something you’ll run into often, but definitely be aware of it. It makes the safe navigation operator not necessarily a drop in replacment.

Javascript Privates

You can utilize ‘private’ features in a javascript class via the # character prepending a name. They are referred to as ‘hash names’.

These private fields must be declared ahead of time otherwise they will result in a syntax error.

For this example I replaced some declaratively ‘private’ attributes _closed & _balance with actual ‘private’ attributes #closed & #balance.

export class BankAccount {
  #closed
  #balance
  
  set balance(amount) {
    return this.#balance = amount;
  }

  get opened() {
    return !this.closed;
  }

  get closed() {
    return this.#closed == true;
  }
}

It appears that if you’re at Node 12 or higher you can use this functionality.

You can read more about this at MDN

Nullish coalescing operator - ??

Ever need to have a back up default value when something is null or undefined in Javascript? Well a fairly recent addition to JS has got you covered. The Nullish coalescing operator?? may help you on your logical path.

If the first half of the epresssion is ‘nullish’, it will return the latter.

const defaultValue = 'I WIN'
const someVariable = null
someVariable ?? defaultValue
=> 'I WIN'

compare this to using || in which the first half of expression needs to evaluate falsey to get into the latter portion.

As it is a newer part of the JS API make sure to check for browser support. There is also a polyfill available.

Automate Applescript via Accessibility Description

While automating a few tasks I do frequently it became apparent that many apps do not title their UI elements. This makes it more of a trial and error process to figure out which button is where.

This leads to script that is not very descriptive:

tell application "System Events"
    tell process "zoom.us"
        click button 3 of window 0
    end tell
end tell

I’ve always wanted to reference elements by their label or utilize accessibility attributes if they are available but could never figure out how.

I finally have!

tell application "System Events"
    tell process "zoom.us"
        click (first button where its accessibility description = "Copy Invite Link") of window 0
    end tell
end tell

This is obviously a bit more verbose but infinitly more descriptive when looking back at what a script is doing.

My future self is already thanking me.

Tmux not starting after an upgrade

Recently upgraded my version of Tmux and after could no longer start a session. After banging my head on tmux’s config files it turned out that sessions that were currently in play were the problem.

So if you find yourself in this situation… make sure to stop all sessions and most likely you’ll have Tmux working again.

An easy way to kill everything would be tmux kill-server.

Safe Navigation on nil gotcha

tldr; Beware, both ActiveSupport’s try and Ruby’s safe navigation operator &. will always return nil when operating on a nil.

For example, I would have expected either to return a response like so:

> nil.try(:to_s) => ""
> nil&.to_s => ""

However this is not the case:

> nil.try(:to_s) => nil
> nil&.to_s => nil

Rounding Time in Increments

I Recently needed to round down a time in certain increments. This is not something I’ve ever been asked to do previously so it was an interesting problem to figure out. The resulting formula is simple:

TIME_MIN - (TIME_MIN % INCREMENT)

This will give you the previous incremental minute. ex: Given a 5 minute increment @ 3:12, the result would be 10.

time = Time.now
new_min = time.min - (time.min % 5)

I happen to be solving this in a project with ActiveSupport so there was access to Time#change so I finished this up with:

time.change(min: new_min)

Turn on coordinates in Minecraft Realm

In a Minecraft Realm the option to turn on the coordinate system is not available without turning on cheats. That is not good because then you’ll lose achievements.

Many tutorials recommend downloading the realm locally, flipping the switch for coordinates, and then uploading that copy of your world back to the realm. Turns out that some server commands will work in the Realm… coordinates being one of them!

Enter this into your chat window to enable coordinates:

/gamerule showcoordinates true

You can, of course, turn them back off via:

/gamerule showcoordinates false

Find that change you know you made!

Sometimes you do awesome work…

Sometimes you even remember that awesome work when at a later point it is not there but everything in your being knows that you wrote it already.

Hopefully this will help:

git log --all --source -- path/to/my/file

This super useful command will show you changes on a particular file (that you totally know you changed) across branches; because sometimes our brains work so hard that we forget to get something merged and those important changes just sit there waiting to be useful again on another branch.

Keeping an SSH connection alive

Do you get disconnected from your SSH session often? I do… but I’ve found a solution that helps.

An SSH configuration that can be made on the server or client side but in my instance it makes more sense for the update to be on the client side.

In your ~/.ssh/config we’ll utilize the ServerAliveInterval declaration.

ServerAliveInterval
             Sets a timeout interval in seconds after which if no data has
             been received from the server, ssh(1) will send a message through
             the encrypted channel to request a response from the server.  The
             default is 0, indicating that these messages will not be sent to
             the server.  This option applies to protocol version 2 only.

This declaration informs the client to send a keep alive packet at a certain interval to the server, ensuring the connection stays open.

Host example
  Hostname example.com
  ServerAliveInterval 30

That declaration will send a packet every 30 seconds if the connection goes idle. Now this is the desired funcationality that I’d like to see for all of my SSH connections so we can add it to the host wildcard like so:

Host *
  ServerAliveInterval 120

Truncate an Array

Every time I go to truncate and array in JavaScript, I have to look up the syntax. It’s something that makes me angry.

Did you know that there is an oddly easier way to do it without reaching for splice() or slice()?

const collection = [4,6,9,1,12,42];
collection.length = 3;

collection is now [4,6,9]

Hopefully I can remember this one!

Install `capybara-webkit`

Turns out capybara-webkit’s has a dependency on qtwebkit which has been removed from the qt library package. You’ll have the pleasure of finding this out when you run into this grand error:

Project ERROR: Unknown module(s) in QT: webkitwidgets

To get around this, you can install a previous version of qt using brew via:

brew install qt@5.5

Now reload your terminal and ensure that you can run qmake. Once that is all good, go ahead and reinstall/bundle capybara-webkit.

Don’t let this stump you for longer than it did me!

Using pessimistic gem version to catch betas

The trouble with pessimistic (~>) versioning is that it still won’t catch pre-release library versions… or will it.

It actually can allow ‘beta’ versions by adding an ‘x’ to the version you’d like to catch.

gem "rails", "~> 5.x"

Since ‘beta’ versions (5.0.0.beta3) actually register as higher than 5.0.0, the pessimistic operator will not catch the beta version. Gem versioning shows any version with a letter as ‘pre-release’ which is how beta3, rc1, alpha versions work.

Using the ‘x’ as a wildcard will allow pre-release gem versions to be caught.