Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / stream_each.rb
Last active August 22, 2019 16:14
Stream results from the database into Ruby objects
require 'ostruct'
Post = Comment = User = OpenStruct
class PostsWithComments
def call
results = Model.connection.execute <<~SQL
SELECT
posts.id,
posts.title,
@jgaskins
jgaskins / app.cr
Created August 10, 2019 21:48
Roda-like web framework in Crystal
class App
# Provides the `call(context : HTTP::Server::Context)` interface
include HTTP::Handler
# Provides the `route` method that we pass the context to to be
# able to use routing and response data
include Route
def call(context : HTTP::Server::Context)
route context do |r, response, session|
@jgaskins
jgaskins / connection.cr
Last active June 23, 2019 19:11
Neo4j::Bolt::Connection#exec_cast with a block
def exec_cast(query : String, parameters : Map, types : Tuple(*TYPES), &block) forall TYPES
send Commands::Run, query, parameters
send Commands::PullAll
result = read_raw_result
until result[1] != 0x71
# First 3 bytes are Structure, Record, and List
# TODO: If the RETURN clause in the query has more than 16 items,
# this will break because the List byte marker and its size won't be
# in a single byte. We'll need to detect this here.
@jgaskins
jgaskins / call_metrics.rb
Last active March 24, 2019 00:44
Trace metrics for methods defined in Ruby code
require 'pp'
require 'set'
module CountThings
COUNTS = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = 0 } }
TIMINGS = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] } }
TRACED_METHODS = Hash.new { |h, k| h[k] = Set.new }
module_function
@jgaskins
jgaskins / hooks.rb
Last active February 16, 2019 13:16
Hooks implementation for Clearwater
module Hooks
def self.component *attrs, &render
Class.new(Component) do
attr_reader *attrs
define_method :initialize do |**props|
super()
attrs.each { |attr| `self[#{attr}] = #{props[attr]}` }
end
@jgaskins
jgaskins / sort-videos.js
Created December 11, 2018 14:06
Sort Confreaks event videos by number of views
[...document.querySelectorAll('.video-info')]
.sort((a, b) => (
a.querySelector('.video-views strong').innerText|0) >= (b.querySelector('.video-views strong').textContent|0)
? -1
: 1
).map(v => v.querySelector('.video-title').innerText + ' by ' + v.querySelector('.video-presenters').innerText)
@jgaskins
jgaskins / benchmark_optimized_uuid.cr
Created November 6, 2018 04:39
Optimizing UUID generation from strings in Crystal
require "uuid"
require "benchmark"
struct UUID
def self.optimized(value : String, variant = nil, version = nil)
bytes = uninitialized UInt8[16]
case value.size
when 36 # Hyphenated
{8, 13, 18, 23}.each do |offset|
@jgaskins
jgaskins / benchmark_if_vs_exception.rb
Created October 21, 2018 17:59
Benchmarking conditionals vs exceptions
require 'benchmark/ips'
def expensive_operation duration=0.001
sleep duration
false
end
Benchmark.ips do |x|
x.report 'simple if' do
if false
@jgaskins
jgaskins / app.cr
Created October 17, 2018 13:15
Forking HTTP server
class PrintPid
include HTTP::Handler
def call(context)
context.response.puts Process.pid
end
end
workers = Array.new(8) do |i|
process = fork do
server = HTTP::Server.new([
@jgaskins
jgaskins / benchmark.rb
Created September 5, 2018 03:00
Benchmark callable vs proc as event handlers
class EventHandler
def call event
end
end
class Eventable
def initialize
@events = Hash.new { |h, k| h[k] = [] }
end