Skip to content

Instantly share code, notes, and snippets.

Avatar
🤔
Enumerating

Scott Tadman tadman

🤔
Enumerating
View GitHub Profile
@tadman
tadman / minimal_falcon_rack_ws_app.rb
Created Mar 24, 2020
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('http://127.0.0.1:3000')
module WebSocketApp
def self.call(env)
@tadman
tadman / method_call-vs-send.rb
Created Oct 15, 2019
Method call vs. send benchmark in Ruby
View method_call-vs-send.rb
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
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)
doc.css('head').remove
puts doc.to_s
__END__
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
tadman / server.rs
Created Jun 23, 2015
Rust MIO Example
View server.rs
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|
begin
# Arbitrary method call
i.floor
true
rescue
# Should rescue from known exception (or Pokemon capture)
@tadman
tadman / db.rake
Created Feb 19, 2015
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
FROM (
SELECT n.nspname AS _schema,
@tadman
tadman / refresh.js
Created Jan 27, 2015
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
tadman / call_methods.rb
Created Jan 27, 2015
Comparison of various block delegation methods in Ruby
View call_methods.rb
#!/usr/bin/env ruby
require 'benchmark'
def with_call(object, &block)
block.call(object)
end
def with_proc(object)
Proc.new.call(object)