Skip to content

Instantly share code, notes, and snippets.

View fractaledmind's full-sized avatar

Stephen Margheim fractaledmind

View GitHub Profile
# Rails production setup via SQLite3 made durable by https://litestream.io/
# Copy this to Dockerfile on a fresh rails app. Deploy to fly.io or any other container engine.
#
# try locally: docker build . -t rails && docker run -p3000:3000 -it rails
#
# in production you might want to map /data to somewhere on the host,
# but you don't have to!
#
FROM ruby:3.0.2
@aleksey-blinov
aleksey-blinov / weighted_random_sampling.md
Created December 29, 2021 09:36 — forked from O-I/weighted_random_sampling.md
[TIx 8] Weighted Random Sampling in Ruby

One of the many reasons I love working with Ruby is it has a rich vocabulary that allows you to accomplish your goals with a minimal amount of code. If there isn't a method that does exactly what you want, it's usually possible to build an elegant solution yourself.

Let's take the example of simulating the rolling of a die.

We can represent a die as an array of its faces.

die = [*?⚀..?⚅]
# => ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]
@hopsoft
hopsoft / README.md
Last active March 29, 2024 18:13
Descriptive messages with basic asserts

Descriptive messages with basic asserts

What if I told you that it's possible to get helpful failure messages from basic asserts using idiomatic equality == checks? No DSLs in sight.

This is a proof of concept to demonstrate that it's possible... mostly to satisfy my own curiosity. The concepts here could theoretically be expanded to provide a useful extension to existing testing frameworks or perhaps lay a foundation for an entirely new one.

This experiment created with Ruby 3.0.1. Note to self: There's probably a way to do this with TracePoint instead of monkey patching but I couldn't figure out how to get a reference to the passed variable being compared.

Examples:

@hopsoft
hopsoft / Dockerfile
Last active May 17, 2023 19:58
Dockerize your Rails app
FROM ruby:3.0-alpine
RUN apk add --no-cache --update \
ack \
bash \
build-base \
curl \
git \
htop \
less \
@leastbad
leastbad / application_controller.rb
Created March 25, 2021 16:05
Notifications are fun (Devise, Bootstrap 5, Notyf, FontAwesome)
class ApplicationController < ActionController::Base
include CableReady::Broadcaster
include Toastable
end
@rickychilcott
rickychilcott / slim_select_controller.js
Last active February 28, 2025 12:43
Slim-select stimulus controller
import { Controller } from "stimulus"
import SlimSelect from "slim-select"
import "slim-select/dist/slimselect.min.css"
import "../style/slimselect-customized.css"
export default class extends Controller {
connect() {
const limit = this.data.get("limit")
const placeholder = this.data.get("placeholder")
const searchText = this.data.get("no-results")
@jbranchaud
jbranchaud / switch_pg_server_client_versions.bash
Last active January 15, 2025 00:22
Bash function for switching asdf postgres versions and stopping/starting servers
function switch_pg {
local version_to_run=$1
local currently_running_version=$(psql --no-psqlrc -t -c 'show server_version;' postgres | xargs)
# check if you're erroneously switching to the same version
if [ "$version_to_run" = "$currently_running_version" ]; then
echo "Postgres $version_to_run is already running."
return 1
fi
@beauraF
beauraF / .github | workflows | ci.yml
Last active April 26, 2022 21:00
Simple CI workflow for Rails that run minitest and brakeman on Github Actions
name: CI
on: [push]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
@fractaledmind
fractaledmind / sorter.rb
Last active April 16, 2024 15:08
A generalized sorting implementation that allows for multi-column, multi-directional, nil-handling, case_insensitive, normalized, and/or natural sorting (with tests)
require 'date'
class Sorter
def initialize(*instructions)
@defaults = {
direction: :ascending,
nils: :small,
accessor: :itself,
case_sensitive: true,
normalized: false,