Skip to content

Instantly share code, notes, and snippets.

View mrinterweb's full-sized avatar

Sean McCleary mrinterweb

View GitHub Profile
@mrinterweb
mrinterweb / proc_block_lamda_bench.rb
Created September 26, 2024 00:39
Ruby proc, block, lambda benchmark
require 'benchmark/ips'
def b = yield() + 1
l = -> (x) { x + 1 }
p = proc { |x| x + 1 }
Benchmark.ips do |x|
x.report('block') { b { 1 } }
x.report('lambda') { l.call(1) }
x.report('proc') { p.call(1) }
@mrinterweb
mrinterweb / benchmark.rb
Created June 28, 2024 00:24
faker vs ffaker benchmark
require 'faker'
require 'ffaker'
require 'benchmark/ips'
Benchmark.ips do |x|
x.warmup = 2
x.report("Faker::Name.name") { Faker::Name.name }
x.report("FFaker::Name.name") { FFaker::Name.name }
x.compare!
end
@mrinterweb
mrinterweb / object_instantiation_bench.rb
Last active June 1, 2024 23:56
Ruby benchmark OpenStruct vs Hash vs Class vs Struct vs Data
That is 266 microseconds vs 2 microseconds. When compared OpenStuct is a lot slower, but when used infrequently, microseconds are minimal.
Just don't use OpenStruct where it will be instantiated a lot or in a performance critical spot in your app.
@mrinterweb
mrinterweb / gift_name_draw.rb
Created November 26, 2021 23:03
Script for gift name draw
require 'pp'
NAMES = %w[Frank Sean Bryn Quinn Beckett Katie Brent Brook Katelynn Adrian].freeze
pickers = NAMES.clone
available = NAMES.clone.shuffle
matches = {}
pickers.each do |picker|
picked = available.shift
@mrinterweb
mrinterweb / sb.rb
Created February 23, 2021 22:21
Easy fuzzy branch switching
#! /usr/bin/env ruby
# Switch to git branch by matching part of a name
results = `git branch -l | grep -i #{ARGV[0]} | cut -f 1`
branchNames = results.split("\n").map { |bn| bn.sub(/^\*/, '').strip }
if branchNames.length == 1
`git checkout #{branchNames.first}`
else
puts 'More than one branch name matched'
@mrinterweb
mrinterweb / vultr-create-docker-server.sh
Created January 31, 2020 08:53
Creating, connecting to, and destroying a server on Vultr
#!/bin/bash
set -e
# creates in Seattle using 2 core, 2GB RAM, 64GB storage on Ubuntu 19.04
# note I was using a custom script to install docker. Basically "snap install docker"
vultr-cli server create --region 4 --plan 401 --os 338 --script-id <redacted> --label="docker-dev"
echo ""
echo "Wait about a minute to auto-connect..."
#!/bin/sh
set -e
set -u
set -o pipefail
service_name="${1:-monolith}"
container_id="$(docker container ls | grep "opal_$service_name" | head -n1 | cut -d " " -f 1)"
docker logs -f $container_id
This file has been truncated, but you can view the full file.
Date/Time: 2020-01-21 22:46:49 -0800
End time: 2020-01-21 22:47:10 -0800
OS Version: Mac OS X 10.15.2 (Build 19C57)
Architecture: x86_64h
Report Version: 29
Data Source: Stackshots
Shared Cache: 0x6b67000 1C9C4B5C-0A12-39CC-965C-B305BF7F36AA
Command: VimR
version: "2"
options:
verbose: true
syncs:
rails-app-sync:
compose-dev-file-path: 'docker-compose-dev.yml'
notify_terminal: false
src: './'
sync_strategy: 'native_osx'
sync_host_ip: '127.0.0.1'
# This is a ActiveRecord STI class
class Hyundai < Car
has_one :hyundai_details
delegate *HyundaiDetails::ACCESSIBLE_ACCESSOR_METHODS, to: :hyundai_details
default_scope { includes(:hyundai_details) }
def initialize(args = {})
hyundai_details_attrs = {}
args.each do |key, _val|