Skip to content

Instantly share code, notes, and snippets.

View tadman's full-sized avatar

Scott Tadman tadman

View GitHub Profile
tadman / minimal_falcon_rack_ws_app.rb
Created March 24, 2020 06:16
Minimal Falcon + Rack + WebSocket Adapter Demo
View minimal_falcon_rack_ws_app.rb
require 'async'
require 'async/http/endpoint'
require 'async/websocket/adapters/rack'
require 'falcon'
websocket_endpoint = Async::HTTP::Endpoint.parse('')
module WebSocketApp
tadman / method_call-vs-send.rb
Created October 15, 2019 16:29
Method call vs. send benchmark in Ruby
View method_call-vs-send.rb
require 'benchmark' do |bm|
repeat = 10000000'send') { repeat.times { 5.send(:>, 2) } }'') { repeat.times { 5.method(:>).call(2) } }
# user system total real
View common.rb
class String
def substrings
((0...length).to_a.combination(2).map do |from,to|
# Extract substrings
self[from, to]
end + self.chars).map do |s|
# Trim off any unwanted characters
s.sub(/\A\W+/, '').sub(/\W+\z/, '')
end.sort.uniq.reject do |s|
# Remove empty strings
View decapitate.rb
require 'nokogiri'
doc = Nokogiri::HTML(DATA)
puts doc.to_s
View case_vs_if_vs_hash.rb
#!/usr/bin/env ruby
require 'benchmark'
COUNT = 1000
LOOP = 1000
def case_method_rb
"def case_method(x)",
tadman /
Created June 23, 2015 17:05
Rust MIO Example
extern crate mio;
use std::error::Error;
use mio::*;
use mio::tcp::{TcpListener, TcpStream};
// Setup some tokens to allow us to identify which event is
// for which socket.
const SERVER: Token = Token(0);
View are_okay.rb
def are_okay?(list)
list.all? do |i|
# Arbitrary method call
# Should rescue from known exception (or Pokemon capture)
tadman / db.rake
Created February 19, 2015 21:51
Postgres Database Sequence Resync Rake Task
View db.rake
namespace :db do
namespace :sequences do
desc 'Brings Postgres database sequences into sync with MAX(id)'
task sync: :environment do
ActiveRecord::Base.connection.execute("SET search_path TO public")
updates = ActiveRecord::Base.connection.select_rows(%Q{
SELECT 'SELECT setval('''||_schema||'.'||_seq||''', (SELECT MAX('||_column||') FROM '||_schema||'.'||_table||')+1); ' AS updatestring
SELECT n.nspname AS _schema,
tadman / refresh.js
Created January 27, 2015 21:23
Automatic Page Refresher
View refresh.js
var refresh_element = $('#refresh')[0];
if (refresh_element) {
$(refresh_element).data('refresh', 1);
setInterval(function() {
if ($(refresh_element).data('refresh'))
$(refresh_element).load(window.location.href + ' #refresh');
}, 5000);
tadman / call_methods.rb
Created January 27, 2015 19:42
Comparison of various block delegation methods in Ruby
View call_methods.rb
#!/usr/bin/env ruby
require 'benchmark'
def with_call(object, &block)
def with_proc(object)