This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ostruct' | |
Post = Comment = User = OpenStruct | |
class PostsWithComments | |
def call | |
results = Model.connection.execute <<~SQL | |
SELECT | |
posts.id, | |
posts.title, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[...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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
def expensive_operation duration=0.001 | |
sleep duration | |
false | |
end | |
Benchmark.ips do |x| | |
x.report 'simple if' do | |
if false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventHandler | |
def call event | |
end | |
end | |
class Eventable | |
def initialize | |
@events = Hash.new { |h, k| h[k] = [] } | |
end |