Skip to content

Instantly share code, notes, and snippets.

View hopsoft's full-sized avatar

Nate Hopkins hopsoft

View GitHub Profile
@hopsoft
hopsoft / 00_do_stuff_job.rb
Last active April 18, 2024 00:50
ActiveJob as Service Worker
# ActiveJob natively captures constructor arguments in an `@arguments` instance variable
# which is also exposed as an `arguments` property on each job instance.
#
# Calls to `perform_now` and `perform_later` both forward arguments to the constructor.
#
# For example, all of these invocation styles work.
#
# result = DoStuffJob.new("foobar").perform # sync
# result = DoStuffJob.new.perform("foobar") # sync
# result = DoStuffJob.perform_now("foobar") # sync
@hopsoft
hopsoft / db.rake
Last active April 3, 2024 13:13
Rails rake tasks for dump & restore of PostgreSQL databases
# lib/tasks/db.rake
namespace :db do
desc "Dumps the database to db/APP_NAME.dump"
task :dump => :environment do
cmd = nil
with_config do |app, host, db, user|
cmd = "pg_dump --host #{host} --username #{user} --verbose --clean --no-owner --no-acl --format=c #{db} > #{Rails.root}/db/#{app}.dump"
end
puts cmd
@hopsoft
hopsoft / README.md
Last active April 2, 2024 15:49
AI Prompt Cheatsheet

AI/Large Language Model Prompt Tuning Cheatsheet

When crafting prompts for AI or LLMs, you can adjust various inputs or variables to tailor the model's responses. Here's a simple guide to understanding what each of these terms means:

  • prompt: The question or statement you provide to the model, essentially telling it what you want to know or do.

    • Example: "What is the weather today?" vs. "Write a poem about the rain."
    • Effect: Directly sets the topic and style of the AI's response.
  • max_tokens: Limits how long the AI's response can be, like setting a maximum number of words or sentences it can use to answer.

  • 50 - Makes the AI provide a concise, often one-sentence reply.

@hopsoft
hopsoft / README.md
Last active March 29, 2024 18:54
stdin → fzf with preview

stdin → fzf with preview

Dependencies

  • ruby
  • ripgrep
  • fzf

Setup

@hopsoft
hopsoft / broadcastable.rb
Created November 22, 2021 19:46
Broadcastable model concern/mixin
# frozen_string_literal: true
module Broadcastable
extend ActiveSupport::Concern
def prepend_operation(options = {})
operation = {html: ApplicationController.render(partial: to_partial_path, locals: locals)}
operation.merge options
end
@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
Created September 7, 2023 14:36
Ruby + SQLite Dockerfile
FROM ruby:3.2.2-alpine
# ============================================================================================================
# Install system packages
# ============================================================================================================
RUN apk add --no-cache --update \
bash \
build-base \
curl \
gcompat \
@hopsoft
hopsoft / README.md
Last active March 29, 2024 18:06
ActiveRecord ETL

ActiveRecord ETL

I created this to help me run benchmarks/comparisons against Universal ID, but it could serve as the foundation for a robust ETL data pipeline... and it's less than 70 LOC right now! 🤯 🚀

It handles the extract and transform parts of an ETL process and supports the following options:

  • only - specify which attributes to include
@hopsoft
hopsoft / build_insert_query.rb
Created March 15, 2024 18:00
Get the SQL for an insert statement from ActiveRecord
# Builds an SQL insert query for a given record
#
# @param record [ActiveRecord::Base] Record used to build the SQL insert query
# @return [String] SQL insert query
def build_insert_query(record)
columns = record.class.columns.reject { |col| col.name == record.class.primary_key }
values = columns.map { |col| record[col.name] }
insert_manager = Arel::InsertManager.new
insert_manager.into(record.class.arel_table)
insert_manager.insert(columns.zip(values)).to_sql
@hopsoft
hopsoft / install-ruby.sh
Created May 21, 2019 16:13
Install ruby with rbenv and jemalloc on ubuntu
sudo apt-get update
sudo apt-get install libjemalloc-dev
RUBY_CONFIGURE_OPTS='--with-jemalloc' rbenv install 2.6.3
# test (look for jemalloc warnings)
MALLOC_CONF=invalid_flag:foo ruby -v