Skip to content

Instantly share code, notes, and snippets.

shalvah / game.rb
Last active February 18, 2023 12:45
Solving a logic puzzle using a graph.
# A man needs to get three items across a river:
# a goose, a big bag of beans, and a fox.
# If left alone unsupervised with the beans, the goose will eat the beans.
# If left alone unsupervised with the goose, the fox will eat the goose.
# Only the man and one other item are all that will fit in the boat.
# Can the man get across the river with their goose, beans, and fox?
require 'set'
shalvah /
Last active November 5, 2022 12:10 — forked from damien-roche/
A Primer on Ruby Method Lookup

A Primer on Ruby Method Lookup

Forked from @damien-roche's gist. Here's my addendum.

Method lookup is a simple affair in most languages without multiple inheritance. You start from the receiver and move up the ancestors chain until you locate the method. Because Ruby allows you to mix in modules and extend singleton classes at runtime, this is an entirely different affair.

I will not build contrived code to exemplify the more complicated aspects of Ruby method lookup, as this will only serve to confuse the matter. If you are having trouble following method lookup in your own programs, it is not because Ruby has strange rules (it does), it is because your code is too tangled.

When you pass a message to an object, here is how Ruby finds what method to call:

$orderList = [
'Scully' => [
'friend' => [
shalvah / format.js
Last active January 20, 2022 14:08
Custom serialization format
// Note: this uses Node.js' `Buffer`, so it might not work in the browser
// You can use a polyfill or write yours.
const RED = [0xFF, 0, 0, 0xFF]; // R, G, B, A
const GREEN = [0, 0xFF, 0, 0xFF];
function serialize(val, outputBytes = []) {
if (typeof val === "string") {
serializeString(val, outputBytes);
} else {
require 'algorithms'
require 'fiber'
class EventLoop
def initialize
@queue =
def run(&entrypoint)
shalvah / circular-references-control.js
Last active February 18, 2023 12:53
Testing garbage collection of circular references in PHP and JS. Thread:
// Make sure to run with node --expose-gc
// Control script for the circular references test
// We set circular references, but we remove them explicitly before removing the parent object
// On my machine, memory usage increases by around +1kB at first, then "stabilises" and increases more slowly
(async () => {
while(1) {
let a = {};
let b = {};
a.f = b;
* Simple promise implementation. Doesn't have all the features of the actual Promise API, + assumes you know what you're doing.
class ToyPromise {
constructor(cb) {
this.state = 'pending';
this._promiseResult = undefined;
this._fulfillmentCallbacks = [];
this._rejectionCallbacks = [];
shalvah / .NET Activity IDs
Created July 23, 2020 14:26
Examples of System.Diagnostics.Activity structure in .NET
var Jimp = require('jimp');
const fileName = process.argv[2];
const width = process.argv[3];
const height = process.argv[4];
.then(photo => {
let colours = {};
for (let x = 0; x < width; x++) {
const wordList = {
sacrilege: {popularity: 0.5207411547},
sarcasm: {popularity: 0.1913595276},
said: {popularity: 0.736293578},
sardine: {popularity: 0.2439570583},
sars: {popularity: 0.6371759558},
saw: {popularity: 0.1695272778},
say: {popularity: 0.8312312658},
scare: {popularity: 0.3623531581},