Skip to content

Instantly share code, notes, and snippets.

View brandondrew's full-sized avatar

Brandon Zylstra brandondrew

  • UC Berkeley
  • the tubes
View GitHub Profile
@joeldrapper
joeldrapper / fuzzy_index.rb
Created March 27, 2025 11:20
Simple fuzzy index with left weight
class FuzzyIndex
def initialize
@index = Hash.new { |h, k| h[k] = Set.new }
end
def []=(key, value)
trigrams(key).each { @index[it] << [key, value] }
end
def [](query)
@peterc
peterc / CONVENTIONS.md
Last active April 20, 2025 21:22
CONVENTIONS.md file for AI Rails 8 development
  • You MUST NOT try and generate a Rails app from scratch on your own by generating each file. For a NEW app you MUST use rails new first to generate all of the boilerplate files necessary.
  • Create an app in the current directory with rails new .
  • Use Tailwind CSS for styling. Use --css tailwind as an option on the rails new call to do this automatically.
  • Use Ruby 3.2+ and Rails 8.0+ practices.
  • Use the default Minitest approach for testing, do not use RSpec.
  • Default to using SQLite in development. rails new will do this automatically but take care if you write any custom SQL that it is SQLite compatible.
  • An app can be built with a devcontainer such as rails new myapp --devcontainer but only do this if requested directly.
  • Rails apps have a lot of directories to consider, such as app, config, db, etc.
  • Adhere to MVC conventions: singular model names (e.g., Product) map to plural tables (products); controllers are plural.
  • Guard against incapable browsers accessing controllers with `allo
# config/initializers/phlex_template_handler.rb
require "action_view"
require "phlex"
# Intercept unknown "capitalized" method calls (e.g., PageView(...)) in templates,
# look up a Phlex component class, instantiate it, and render it.
# Crucially, we re-bind the user’s block so that `self` is the component, not the ActionView context.
module PhlexDynamicMethodCalls
def method_missing(name, *args, **kwargs, &block)
# Only intercept method calls starting with an uppercase letter (e.g. "PageView", "MyComponent", etc.)
@ShayneP
ShayneP / basic_agent.py
Created January 28, 2025 20:41
LiveKit, DeepSeek, and Groq
import asyncio
import logging
import os
import aiohttp
from typing import Annotated
from dotenv import load_dotenv
from livekit import rtc
from livekit.agents import (
AutoSubscribe,
JobContext,
@peterc
peterc / sinatra-react.md
Last active February 20, 2025 18:20
How to set up a basic Sinatra + React webapp

How to set up a basic Sinatra + React webapp in 2025

Let's say you want to use Ruby for the backend of a basic webapp but React on the frontend. Here's how.

(Note: All tested on January 13, 2025 with Ruby 3.3, Sinatra 4.1.1, and React 18.3. Configs may change over time.)

First, create the app folder and set up Sinatra:

mkdir my-sinatra-react-app
@darinwilson
darinwilson / one_file_rails_test.rb
Last active December 19, 2024 22:31
A single file that can run minitest within a full Rails environment - good for reproducing simple bugs
# frozen_string_literal: true
#
# to run this file:
# - bundle install
# - ruby one_file_rails_test.rb
#
# original source: https://discuss.rubyonrails.org/t/is-it-ever-ok-to-validate-the-presence-of-a-primary-key-of-a-belongs-to-association/84904
#
# In your Gemfile
gem "localhost"

# Then, depending on your desired server
gem "falcon"
gem "puma"
@kaspth
kaspth / concern.rb
Created June 4, 2024 20:37
Writing Concerns without the `extend`/`included`/`class_methods` boilerplate.
# Maybe there's a way to support ActiveSupport::Concern's dependencies too?
# Although this doesn't use the module hierarchy, so `prepend` can't work.
class Concern < Module
def initialize(&block) = @block = block
def included(klass) = klass.class_eval(&@block)
end
module Kernel
def Concern(...) = Concern.new(...)
end
#!/usr/bin/env bash
# Abort sign off on any error
set -e
# Start the benchmark timer
SECONDS=0
# Repository introspection
OWNER=$(gh repo view --json owner --jq .owner.login)
@brandondrew
brandondrew / button_progress_controller.js
Created February 22, 2024 03:20 — forked from dbreunig/button_progress_controller.js
Poor man's form button progress indicator, in Stimulus, for those long processing requests.
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
connect() {
addEventListener("turbo:submit-end", ({ target }) => {
clearInterval(this.interval);
this.element.innerHTML = this.originalLabel;
})
this.originalLabel = this.element.innerHTML;
}