Skip to content

Instantly share code, notes, and snippets.

View noteflakes's full-sized avatar

Sharon Rosner noteflakes

View GitHub Profile
# frozen_string_literal: true
require "./lib/extralite"
require "benchmark"
require "tempfile"
require "fileutils"
p sqlite_version: Extralite.sqlite3_version
N = (ENV['N'] || 1000).to_i
@noteflakes
noteflakes / loop_vs_while.rb
Created July 3, 2023 20:57
Which is faster, loop or while?
# frozen_string_literal: true
require "benchmark/ips"
def loop_run(count)
x = 0
loop do
x += 1
break if x >= count
end
@noteflakes
noteflakes / chat.rb
Last active November 18, 2021 22:15
Real-world Concurrency with Ruby and Polyphony: a Telnet-based Chat App
# See also: https://noteflakes.com/articles/2021-11-13-real-world-polyphony-chat
require 'polyphony'
server = spin do
server_socket = TCPServer.new('0.0.0.0', 1234)
server_socket.accept_loop do |s|
spin { handle_session(s) }
end
end
@noteflakes
noteflakes / losing_my_mind_on_concurrency.rb
Last active May 11, 2021 07:13
A script showing how to use Polyphony to perform HTTP requests concurrently from a queue of jobs
# "I'm losing my mind on concurrency"
# https://www.reddit.com/r/ruby/comments/lmg8xo/im_losing_my_mind_on_concurrency/
require 'polyphony'
require 'httparty'
require 'json'
CHANNELS = ['list', 'of', 'channels']
JOB_QUEUE = Queue.new
TIMEOUT = 60 # 60 second timeout
@noteflakes
noteflakes / polyphony_tipi_websocket_demo.rb
Last active July 28, 2020 19:41
A brief demo showing Websocket client and server capabilities using Polyphony and Tipi
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'polyphony', '~> 0.44'
gem 'tipi', '~> 0.31'
end
@noteflakes
noteflakes / sourdough_bread.md
Last active September 1, 2019 07:10
Sourdough Bread Recipe

Sourdough bread for home baking

The following recipe is the product of a few years of "research" in my kitchen. This recipe continues to evolve basically each time I bake, tweaking quantities, durations and techniques as experience and knowledge are gained. I put it here in digital form for all to enjoy.

Bread is such a fascinating subject (yeah I'm geeking out here) and is so rewarding to make - and eat. Bread is at once earthly and heavenly. Once you start "feeling" the dough, once you realize the dough is a living, breathing

@noteflakes
noteflakes / weighted-avg.sql
Created May 3, 2018 06:48
Calculate weighted average over irregular time series
create table samples (
stamp timestamptz,
series integer,
value float
);
insert into samples values
('2018-04-30 23:00:00+02', 1, 12.3),
('2018-05-01 01:45:00+02', 1, 22.2),
('2018-05-01 02:13:00+02', 1, 21.6),
@noteflakes
noteflakes / pgstore.rb
Last active July 6, 2023 18:36
A lightweight PostgreSQL ORM using JSONB. Provides an API for retreiving documents by arbitrary key, and performing queries on arbitrary keys and sub-keys.
require 'pg'
PGDB = PG.connect(host: '/tmp', dbname: 'mydb')
PGDB.type_map_for_results = PG::BasicTypeMapForResults.new(PGDB)
class Hash
def symbolize_keys
inject({}) { |m, kv| v = kv[1];
m[kv[0].to_sym] = v.is_a?(Hash) ? v.symbolize_keys : v; m }
end
end