Skip to content

Instantly share code, notes, and snippets.

View sambostock's full-sized avatar
💬
Sam is typing...

Sam Bostock sambostock

💬
Sam is typing...
View GitHub Profile
@sambostock
sambostock / README.md
Created October 31, 2023 00:39
Heredoc indentation cop

Heredoc Indentation Cop

This is a scrappy implementation of a cop that checks the following for heredocs:

  • Contents may only be indented in increments of 2 spaces.
  • Contents may not be indented using spaces.
  • Lines may be at max indented 2 spaces more than the line before, ignoring blank lines.

Usage

  1. Copy heredoc_internal_indentation.rb to wherever you want in your project
@sambostock
sambostock / README.md
Created October 27, 2022 01:08
Faraday linewise streaming example

Overview

Say you're trying to consume a streaming API using Faraday. And say you need to consume the content line-by-line.

Let's explore one way of doing that.

How it works?

  1. Faraday allows setting .options.on_data on the request to a Proc to run as data is streamed.
@sambostock
sambostock / README.md
Last active June 3, 2022 05:34
Ontario 2022 Election Margins
@sambostock
sambostock / minitest_parent.rb
Last active March 16, 2022 19:38
Naively forbidding inheritance from classes defining tests
require 'minitest/autorun'
module ForbidSubclassingIfTestMethodsDefined
DuplicateTestsError = Class.new(StandardError)
def inherited(klass)
unless runnable_methods.empty?
raise DuplicateTestsError, "Do not inherit from #{self}, as the tests it defines would be duplicated in #{klass}"
end
super
@sambostock
sambostock / README.md
Created November 17, 2020 23:17
Naive Ractor solution to RubyConf 2020 Raffle

Raffle Ractor

RubyConf 2020 held a puzzle-raffle in which the puzzle required using brute force to figure out the cleartext which produced a given hash.

Problem Description

If you process payments with Braintree you’ve likely seen a unique id that looks something like a1b2c3d4. This id goes by different names at different places but is often a way for a company to create a massive numbering system. Assuming an alphabet of only lower case letters and digits, the system above could be used for (26 + 10) ^ 8 = 2,821,109,907,456 combinations. That is an enormous number! Some places, including Braintree, also choose to encode some information in these ids. Without going too much into the Mathematics of hashing and encoding algorithms we can say that it’s possible to take some information, let’s say an email or url, add some other information, say a 5 character raffle number 😉, and encode that information into a fixed length String. If you know the unique id and you have the fixed length encoded Stri

@sambostock
sambostock / rescue_hack.rb
Created June 22, 2020 10:42
Unexpected behaviour with Ruby's rescue allowing calling of any setter method instead of local variable assignment.
explicit_receiver = Object.new
def explicit_receiver.setter=(error)
puts "setter= called with #{error.class}"
end
begin
1 / 0
rescue => explicit_receiver.setter
# There doesn't seem to be a restriction on rescue setting local variables.
# Ruby seems to take whatever appears in the rescue clause
@sambostock
sambostock / scheduler.rb
Last active April 28, 2023 18:57
Oncall scheduler script for PagerDuty - Given a list of junior and senior developers to be added to primary and secondary schedules, finds a suitable set of pairings.
# frozen_string_literal: true
Dev = Struct.new(:name, :senior, :prefers_consecutive_shifts, keyword_init: true) do
def junior?
!senior
end
def short_name
first, last = name.split(' ', 2)
return first if last.nil?
@sambostock
sambostock / react-key.md
Last active September 26, 2019 00:19
Keys in React and Vue

React Keys

Consider the difference between these:

function NameList({names}) {
  return (<ul>{names.map((name, index) =>
    <li key={index}>{name}</li>
  )}</ul>);
}
@sambostock
sambostock / Gemfile
Created February 15, 2019 04:14
Benchmarking Timecop vs ActiveSupport::Testing::TimeHelpers
# frozen_string_literal: true
ruby '2.5.3'
source "https://rubygems.org"
gem 'timecop'
gem 'activesupport'
@sambostock
sambostock / ClassBasedToggle.jsx
Created October 29, 2018 02:26
Trivial React State Hook example
import { Component } from 'react'
export default ClassBasedToggle extends React.Component {
constructor(props) {
super(props)
this.state = {
on: false,
}