Skip to content

Instantly share code, notes, and snippets.

View hoffm's full-sized avatar
☀️
twib.nyc

Michael Hoffman hoffm

☀️
twib.nyc
View GitHub Profile
@hoffm
hoffm / pair-commit.sh
Created May 15, 2023 15:48
Script for automating co-authored commit messages to facilitate pair programming
#!/usr/bin/env bash
# Based on https://github.com/spinningarrow/git-coauthors/
# Runs "git commit" and initializes the commit editor with a co-authorship trailer
# matching the argument you provided. Will only search past commiters to the current
# repo and branch.
#
# $ pair-commit melba
# -> Opens up commit editor populated with the following trailer:
class Trie < Hash
def initialize
# Ensure that this is not a special Hash by disallowing
# initialization options.
super
end
def add(string)
string.chars.inject(self) do |trie, char|
trie[char] ||= Trie.new
include Quicksort
RSpec.describe 'Quicksort' do
describe '#partition!' do
it 'partitions the p..r subarray around a pivot' do
array = [9, 7, 5, 11, 12, 2, 14, 3, 10, 4, 6]
pivot = partition!(array, 0, array.length - 1)
expect(pivot).to eq(4)
expect(array).to eq([5, 2, 3, 4, 6, 7, 14, 9, 10, 11, 12])
@hoffm
hoffm / quicksort.rb
Last active September 12, 2022 19:01
module Quicksort
module_function
# Sorts in place the subarray of `array` from index `first`
# to index `last`, inclusive.
#
# @param array [Array] the array to be (partially) sorted
# @param first [Integer] the left index of the subarray to be sorted
# @param last [Integer] the right index of the subarray to be sorted
# @return [Array] the sorted array
@hoffm
hoffm / grape_sti_colllections.rb
Last active May 21, 2018 15:23
Monkey patch to allow Grape's `present` method to represent list of objects with heterogeneous entities
# ###
# # OVERVIEW
# ###
#
# Monkey patch of Grape DSL to augment behavior in cases where
# we are presenting lists of objects of different types. The
# primary use case for this is the presentation of lists of
# records that inherit from an abstract parent, i.e. single-
# table inheritance.
#
class MyClass
def foo_via_method
p "Superclass Chain: #{self.class.ancestors}"
p "Lexical Scope Chain: #{Module.nesting}"
foo_method
end
def foo_via_constant
p "Superclass Chain: #{self.class.ancestors}"
p "Lexical Scope Chain: #{Module.nesting}"
# THIS WORKS
SubClass.new.foo_via_method
# => "foo"
# THIS DOESN'T
SubClass.new.foo_via_constant
# NameError: uninitialized constant MyClass::FOO_CONSTANT
class StarsController < StoryBooleansController
STORY_BOOLEAN = :starred
end
class StarsController < StoryBooleansController
private
def story_boolean
:starred
end
end
class StoryBooleansController < BaseController
def create
update_story(story_boolean => true)
end
def destroy
update_story(story_boolean => false)
end
private