Skip to content

Instantly share code, notes, and snippets.

View doolin's full-sized avatar

Dave Doolin doolin

View GitHub Profile
@doolin
doolin / code.rb
Created April 18, 2024 11:17 — forked from jjb/code.rb
Hacking ruby patterns to be first-class objects that can be passed around
# need to "pin" the variable, but this only works with the lambda because
# pattern matching falls back to case matchint (=== on any object in ruby,
# which is NOT "identical object" as in JS)
def matches1(pat,x)
case x
in ^pat
puts true
else
puts false
end
@doolin
doolin / falsehoods-programming-time-list.md
Created March 5, 2024 13:31 — forked from timvisee/falsehoods-programming-time-list.md
Falsehoods programmers believe about time, in a single list

Falsehoods programmers believe about time

This is a compiled list of falsehoods programmers tend to believe about working with time.

Don't re-invent a date time library yourself. If you think you understand everything about time, you're probably doing it wrong.

Falsehoods

  • There are always 24 hours in a day.
  • February is always 28 days long.
  • Any 24-hour period will always begin and end in the same day (or week, or month).
@doolin
doolin / file.md
Created March 13, 2023 12:00 — forked from jjb/file.md
Using Jemalloc 5 with Ruby.md

This is a very quick gist I'm throwing together to get this info and discussion onto google in one place.

For years, people have been using jemalloc with ruby. There are various benchmarks and discussions. Some people say Jemalloc 5 doesn't work as well as Jemalloc 3.

Then, one day, hope appeared on the horizon. Someone offered a config for Jemalloc 5.

The recipe would be something like this (shown with official docker ruby image). I didn't try this yet, don't know if it will build!

@doolin
doolin / gist:6e99c0d5317d8316c3619c3eebeb8032
Created January 31, 2023 17:23 — forked from RiANOl/gist:1077760
AES128 / AES256 CBC with PKCS7Padding in Ruby
require "openssl"
require "digest"
def aes128_cbc_encrypt(key, data, iv)
key = Digest::MD5.digest(key) if(key.kind_of?(String) && 16 != key.bytesize)
iv = Digest::MD5.digest(iv) if(iv.kind_of?(String) && 16 != iv.bytesize)
aes = OpenSSL::Cipher.new('AES-128-CBC')
aes.encrypt
aes.key = key
aes.iv = iv
@doolin
doolin / pkey_rsa_converter.rb
Created January 20, 2023 16:20 — forked from anicet/pkey_rsa_converter.rb
PKeyRSAConverter: RSAKeyValue .NET XML to RSA PEM (and RSA PEM to RSAKeyValue .NET XML)
require 'openssl'
require 'base64'
require 'rexml/document'
class PKeyRSAConverter
def initialize(from_pem: nil, from_xml: nil)
@from_pem = from_pem
@from_xml = from_xml
end
@doolin
doolin / csp2pem.rb
Last active December 30, 2022 00:34 — forked from cky/csp2pem.rb
Quick script for converting PEM-format RSA keys to a format usable by .NET's RSA.FromXmlString
#!/usr/bin/ruby
require 'openssl'
TYPES = {
6 => {magic: 'RSA1', private_key: false},
7 => {magic: 'RSA2', private_key: true}
}
data = ARGV[0] ? File.binread(ARGV[0]) : $stdin.binmode.read
type, version, algo, magic, bits, rest = data.unpack('ccx2l<a4l<a*')
class Orders < ActiveRecord::Migration[7.0]
def change
create_table :orders do |t|
t.string :first_name # use Rails validations
t.string :last_name, null: false, limit: 128
end
end
end
Orders.migrate(:up) unless Orders.data_source_exists?(:orders)
class Order
include ActiveModel::Validations
attr_reader :name
validates :name, presence: true
def initialize(name)
@name = name
end
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'active_record'
require 'rspec/autorun'
require 'pg'
require 'byebug'
# Extend for effect
class String
<mxfile host="Electron" modified="2022-03-13T20:44:33.536Z" agent="5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/16.5.1 Chrome/96.0.4664.110 Electron/16.0.7 Safari/537.36" etag="R_SOdd2db7crDnFWafUC" version="16.5.1" type="device"><diagram id="qbLhw_JT5a_EEhQz4K1e" name="Page-1">7Z1td5s2GIZ/jT+6B8T7x8Zu2q5zt9Ns67pvxMg2C0YM48TJr59kwDE8yolpjBBGaY9DBAis5/ItcevFI2Oy3n1M/WQ1IwGORkgLdiNjOkJINxEasf9a8JinuKaXJyzTMCgOek64CZ9wkagVqdswwJvKgRkhURYm1cQ5iWM8zyppfpqSh+phCxJVr5r4SwwSbuZ+BFO/h0G2KlJ1TXve8QmHy1Vxadcqdtz687tlSrZxcb0RMq73P/nutV/mVRy/WfkBeThKMj6MjElKSJZvrXcTHLGyLYstP+/6hb2H+05xnJ1ywuevH4i5w//Ef3z98vTj753zy/ZuXORy70fbojz+TIrbzR7LIsrwjl7hapWtI5qg000/Cpcx3Z7Ta+OUJtzjNAtpob4vdqzDIGCnX6V4Ez75t/usNPp3QsI424fMuhpZU5bXNiObHAuW9SZLyR2ekIjQfKcxiVkuizCK6kkkzgqadJtdiEUCB8Vl2N5rfx1GjMpv5JZkhL6nGYlJeSrZpnN28irLKGnIMt7TF1p47IUdsHm3JGQZYT8JN+/mZL3fMd/sD71e5FnTzUPmFro6yr68WQqFOWX/aDoMWFn6tPDw7iipCOBHTNY4S+l1tGKvZRj5KcWHrWTx4Zlco0haHTFbIugXn5XlIeNnXuhGgUwDfBDAZ0oeYgWQvAChKkCOAwmyRBJkAIKuSfrgp0EDiCK8yDgI0WL