Skip to content

Instantly share code, notes, and snippets.

View tadman's full-sized avatar
🤔
Enumerating

Scott Tadman tadman

🤔
Enumerating
View GitHub Profile
@tadman
tadman / minimal_falcon_rack_ws_app.rb
Created March 24, 2020 06:16
Minimal Falcon + Rack + WebSocket Adapter Demo
require 'async'
require 'async/http/endpoint'
require 'async/websocket/adapters/rack'
require 'falcon'
websocket_endpoint = Async::HTTP::Endpoint.parse('http://127.0.0.1:3000')
module WebSocketApp
def self.call(env)
@tadman
tadman / method_call-vs-send.rb
Created October 15, 2019 16:29
Method call vs. send benchmark in Ruby
require 'benchmark'
Benchmark.bm do |bm|
repeat = 10000000
bm.report('send') { repeat.times { 5.send(:>, 2) } }
bm.report('method.call') { repeat.times { 5.method(:>).call(2) } }
end
# user system total real
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
require 'nokogiri'
doc = Nokogiri::HTML(DATA)
doc.css('head').remove
puts doc.to_s
__END__
#!/usr/bin/env ruby
require 'benchmark'
COUNT = 1000
LOOP = 1000
def case_method_rb
[
"def case_method(x)",
@tadman
tadman / server.rs
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);
def are_okay?(list)
list.all? do |i|
begin
# Arbitrary method call
i.floor
true
rescue
# Should rescue from known exception (or Pokemon capture)
@tadman
tadman / db.rake
Created February 19, 2015 21:51
Postgres Database Sequence Resync Rake Task
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
FROM (
SELECT n.nspname AS _schema,
@tadman
tadman / refresh.js
Created January 27, 2015 21:23
Automatic Page Refresher
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
tadman / call_methods.rb
Created January 27, 2015 19:42
Comparison of various block delegation methods in Ruby
#!/usr/bin/env ruby
require 'benchmark'
def with_call(object, &block)
block.call(object)
end
def with_proc(object)
Proc.new.call(object)