Skip to content

Instantly share code, notes, and snippets.

View sirupsen's full-sized avatar
🐡

Simon Eskildsen sirupsen

🐡
View GitHub Profile
@sirupsen
sirupsen / book.rb
Last active March 5, 2024 20:42
Script to import books from Instapaper to Airtable. Will not work out of the box.
class Book < Airrecord::Table
class Endorser < Airrecord::Table
self.base_key = ""
self.table_name = "Endorser"
end
self.base_key = ""
self.table_name = "Books"
has_many :endorsements, class: 'Book::Endorser', column: 'Endorsements'
require "sqlite3"
require 'set'
require 'byebug'
# Will be rebuilt at any time. Nice and incremental.
db = SQLite3::Database.new "index.db"
# Keep prefix indexes for "mos*" searches.
#
# TODO: It doesn't seem like SQLITE FTS5 supports synonyms well. That's ok, but
# we're going to want that. We can download this database from Princeton, write
@sirupsen
sirupsen / README.md
Created October 15, 2010 15:23
Simple evented ChatServer in pure Ruby using network I/O with select(2).

Dependencies

Colored for chat colors

gem install colored

It has many comments since I tried to explain what is going on for less-experienced Ruby people.

@sirupsen
sirupsen / search.bash
Last active March 5, 2023 14:39
I run all my searches in FZF as I edit my notes in Vim. See https://share.cleanshot.com/x9ZkfBQQ -- Will require some tinkering but it runs the inference/search server so the searches are fast. It's deployed to Modal.com
#!/bin/bash
# FZF quotes field names so I can't easily get preview to work otherwise
cd "$ZK_PATH"
# --bind="2:preview(echo '\"{q}\",\"{4..}\",2' >> judge.csv && echo Rated 2!)" \
FZF_DEFAULT_COMMAND="python ~/src/semsearch/search_webhook.py learning"
fzf \
--bind="0:preview(ruby ~/src/semsearch/write.rb {q} {4..} 0 && echo Rated 0!)" \
--bind="1:preview(ruby ~/src/semsearch/write.rb {q} {4..} 1 && echo Rated 1!)" \
@sirupsen
sirupsen / sidenotes.tsx
Last active January 5, 2022 11:47
Sidenotes and table of contents on MDX with React.
function isColliding(a: DOMRect, b: DOMRect) {
return !(
((a.y + a.height) < (b.y))
|| (a.y > (b.y + b.height))
|| ((a.x + a.width) < b.x)
|| (a.x > (b.x + b.width))
);
}
// Move the footnotes from the bottom to become sidenotes if the viewport is
@sirupsen
sirupsen / hosts.focus
Created March 13, 2013 10:43
My /etc/hosts and the accompanying crontab.
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
switch_strategy = 0
no_switch_strategy = 0
n_simulations = 1_000_000
n_simulations.times do
winner_door = rand(3)
chosen_door = rand(3)
if winner_door == chosen_door
switch_strategy += 0
n_shards = 128
n_simulations = 1_000_000
processed_booted_for_minimum_two_per_shard = []
n_simulations.times do |i| # Number of simulations
booted = 0
booted_by_shard = [0] * n_shards
done = [false] * n_shards
done_count = 0
@sirupsen
sirupsen / attr.rb
Created June 12, 2010 13:12
Explanation of attr_* in Ruby.
# Explanation of attr_* to Noxn
# Let's start with an example class using attr_acessor:
class Klass
attr_acessor :variable
def initialize(content)
@variable = content
end
end
require 'socket'
def internet_checksum(packet) # https://tools.ietf.org/html/rfc1071
packet += [0].pack('C') if packet.size.odd? # since we read 2 bytes at once, append 0 if odd
sum = packet.unpack("n#{packet.size/2}").sum
sum = (sum & 0xffff) + (sum >> 16) while (sum >> 16 > 0) # fold 32 bits -> 16
~sum & 0xffff # 1s complement
end