Skip to content

Instantly share code, notes, and snippets.

View doolin's full-sized avatar

Dave Doolin doolin

View GitHub Profile
@doolin
doolin / tree_enumerator.rb
Created August 17, 2024 18:47 — forked from JoelQ/tree_enumerator.rb
Demonstration of how using `Enumerator` makes it nicer to work with a data structure that has multiple valid traversals such as a binary tree
class Tree
attr_reader :val, :left, :right
def self.empty
EmptyTree.new
end
def self.leaf(val)
new(val, empty, empty)
end
@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