Skip to content

Instantly share code, notes, and snippets.

@adam12
adam12 / gist:acb2b08b7bc1a711fdf0afe503667c2e
Created November 30, 2023 15:31 — forked from rkumar/gist:445735
ruby's OptionParser to get subcommands
#!/usr/bin/env ruby -w
## Using ruby's standard OptionParser to get subcommand's in command line arguments
## Note you cannot do: opt.rb help command
## other options are commander, main, GLI, trollop...
# run it as
# ruby opt.rb --help
# ruby opt.rb foo --help
# ruby opt.rb foo -q
# etc
@adam12
adam12 / packer.rb
Created July 12, 2023 12:58 — forked from joeldrapper/packer.rb
Paquito Job Packer
# frozen_string_literal: true
module Packer
# A special packer class for globally identifiable objects. It takes a global ID and packs it as a String, then rehydrates it as a GlobalID lookup.
class GloballyIdentifiable
def self.from_pack(uri)
GlobalID::Locator.locate(uri)
end
def initialize(identifiable)
@adam12
adam12 / ring_buffer.rb
Created July 7, 2023 16:59 — forked from havenwood/ring_buffer.rb
A ring buffer implementation example for Ruby Discord.
# frozen_string_literal: true
class RingBuffer
Nothing = Data.define
EMPTY = Nothing.new
attr_reader :size
def initialize(capacity:)
@capacity = capacity
@adam12
adam12 / cache.rb
Created November 11, 2022 02:37 — forked from havenwood/cache.rb
Another example for xco on #ruby IRC
require_relative 'tuple_space'
class Cache
def initialize
@memory = TupleSpace.new(reaper_period_in_secs: 10, expires_in_secs: 60)
end
def get(request)
@memory[request]
end
@adam12
adam12 / docker-image-prune.service
Created June 3, 2021 18:13
Docker Image Prune systemd service and timer
[Unit]
Description=Docker Image Prune
[Service]
Type=oneshot
ExecStart=/usr/bin/docker image prune -f
WorkingDirectory=/tmp
@adam12
adam12 / database_cleaner.rb
Created January 9, 2020 02:23 — forked from jsteiner/database_cleaner.rb
Database Cleaner
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
@adam12
adam12 / 1 - I plan to turn these off
Created November 15, 2019 19:43 — forked from dlangille/1 - I plan to turn these off
Periodic things to turn off in FreeBSD jails
# after reviewing /etc/defaults/periodic.conf I have decided
# to disable these items in jails
daily_status_disks_enable="NO"
daily_status_network_enable="NO"
daily_status_uptime_enable="NO"
# not needed on jails
daily_ntpd_leapfile_enable="NO"
@adam12
adam12 / cron_helper.sh
Created August 23, 2019 02:08 — forked from liquidgecka/cron_helper.sh
Cron helper
#!/bin/bash
usage() {
cat << EOF
Usage: $0 [OPTION]... COMMAND
Execute the given command in a way that works safely with cron. This should
typically be used inside of a cron job definition like so:
* * * * * $(which "$0") [OPTION]... COMMAND
Arguments:
@adam12
adam12 / accounts.txt
Created May 8, 2019 00:22 — forked from simonmichael/accounts.txt
a sample *ledger chart of accounts (first 3 levels): combined personal & business, eg for a freelancer
assets
business
accounts receivable
bank
personal
accounts receivable
bank
cash
gifts
online
@adam12
adam12 / lazy.rb
Created April 23, 2019 13:53 — forked from localhostdotdev/lazy.rb
Lazy: fetch deep values only when you need to
# Example: Lazy.new(lambda { |id| API::HackerNews.item(id) }, 0)
class Lazy
attr_reader :value
def initialize(function:, value:)
@function = function
@value = value
@cached = nil
@is_cached = false