Today I Learned

4 posts by cameronwoloshyn

Matching array subset in Ruby

Problem:

How do you evaluate whether one array is a subset of another? For example, are the elements [a,c] included in [a,b,c]?

First attempt:

I was hoping to find something like Array.include?([...]), but this only checks if the array includes the argument as one of its values.

Second attempt:

Another approach is to pass a block into Array.any?

!arr1.any? { |e| !arr2.include?(e) }

But the double negation is rather indirect and doesn't easily reveal the intent.

I considered extracting a method to name the functionality:

def subset?(arr1, arr2)
  !arr1.any? { |e| !arr2.include?(e) }
end

But it's still difficult to read, as it's not clear whether arr1 is a subset of arr2, or vice versa.

Final Solution:

The Enumerable module includes a to_set method to convert the array to set, and Set includes a subset? method.

arr1.to_set.subset?(arr2.to_set)

Technically, you need to require set.rb to get this method defined on Enumberable:

require "set"

arr1.to_set.subset?(arr2.to_set)

But you get this require for free in Rails.

Which Javascript Shell?

When it comes to Javascript Shells, there's a long list to choose from, but typically, when I want to play with Javascript, the easiest way is to simply open up a console in Chrome.

Recently, I've switched over to using SpiderMonkey's Javascript shell because it allows me to execute a JS file and then drop into the shell.

Installation:

$ brew install spidermonkey

Vanilla Use Case:

Open up a shell with js command:

$ js
js> var obj = {a: 1};
js> obj;
({a:1})
js>

How to execute a file and then drop into the shell:

Given a file example.js with the following contents:

var a = 42;

Execute the file (-f) and continue in interactive mode (-i):

$ js -f example.js -i
js> a;
42

Check out the docs for a full list of options.

Add a message to git stash

Problem:

You want to stash a number of patches and add a message to describe what each includes.

Context:

Using git stash without arguments stacks your code changes on the top of a the stash stack, and adds a default message: WIP on branchname …​. If you stash multiple patches, you may find it difficult to recall what each stash includes:

$ git stash list

stash@{0}: WIP on 1234_add_new_feature: 1a2b3c4 #1234 - updates new feature template
stash@{1}: WIP on 1234_add_new_feature: 1a2b3c4 #1234 - updates new feature template

Solution:

You can add a message to the the stash by using the save argument:

$ git stash save "refactors how the UI elements are rendered"
$ git stash list

stash@{0}: On 1234_add_new_feature: refactors how the UI elements are rendered
stash@{1}: WIP on 1234_add_new_feature: 1a2b3c4 #1234 - updates new feature template
stash@{2}: WIP on 1234_add_new_feature: 1a2b3c4 #1234 - updates new feature template


This workflow is especially useful when you're stashing commits while spiking.

Search through git commit messages

Problem:

How do you find the commits associated with a specific story?

Context:

At Nulogy, we use a story tracking system that assigns a number to each story. When we're developing, we tag each commit message with the story number:

$ git commit -m "#1234 - refactors spec"

Solution:

We can use git log --grep, which greps the commit messages in the repo:

$ git log --grep="#1234"