Skip to content

Instantly share code, notes, and snippets.

@sdeming
sdeming / bsearch_index_vs_index_bench.rb
Created June 5, 2018 02:38
Quick simple benchmark between bsearch_index and index with over sorted list of numbers. Binary search is obvious winner. This is just the proof.
require 'benchmark'
ordered_array = (0..10_000).map { |p| [p,p,p,p,p] }.flatten
random_array = ordered_array.dup.shuffle
Benchmark.bm do |b|
b.report('index') do
random_array.map { |n| ordered_array.index(n) }
end
@sdeming
sdeming / a.cc
Last active August 29, 2015 14:15
diamond inheritance in c++...
#include <iostream>
using namespace std;
class A
{
public:
virtual void Hello() { cout << "Hello from A" << endl; }
virtual ~A() {}
};
@sdeming
sdeming / Results
Created December 7, 2012 23:51
Proc wrapped eval, eval only once...
Rehearsal ------------------------------------------------------
proc wrapped eval: 1.020000 0.030000 1.050000 ( 0.605000)
instance_eval: 2.950000 0.030000 2.980000 ( 2.273000)
--------------------------------------------- total: 4.030000sec
user system total real
proc wrapped eval: 0.200000 0.000000 0.200000 ( 0.204000)
instance_eval: 1.800000 0.010000 1.810000 ( 1.767000)
@sdeming
sdeming / gist:4113126
Created November 19, 2012 19:31
Totally Contrived Episode #1 - class << self
class Foo
class << self
attr_accessor :a, :b
end
def self.register(args = {})
args.each do |k,v|
self.send(:"#{k}=", v)
end
end
@sdeming
sdeming / noloops.cc
Created May 25, 2012 15:00
using partial template specialization for pattern matching in c++, simple demo
#include <iostream>
template <int N>
void pr(const std::string &msg)
{
std::cout << msg << std::endl;
pr<N-1>(msg); // would be nice to do tail call optimization here...
}
template <>
@sdeming
sdeming / install.rb
Created May 3, 2012 04:01
Really simple script for installing rails apps when you can't deploy with Capistrano. Just zip up the app and run ./install.rb zipfile
#!/bin/env ruby
ENV['RAILS_ENV'] = 'production'
require 'fileutils'
def sudo(cmd, as = 'root')
puts "sudo: #{cmd}"
system("sudo -u #{as} #{cmd}")
end
@sdeming
sdeming / rubage.rb
Created March 22, 2012 01:37
parens remix
class Rubage
(def foo a, b, c
puts a
puts b
puts c
(puts a.gsub /A/, 'B')
end)
end
(if $0 == __FILE__
@sdeming
sdeming / parasite.rb
Created March 20, 2012 03:29
Sometimes it's fun to give any old object an obscure payload. AKA, a parasite.
module Parasite
def self.infect(object)
(class << object; self end).class_eval do
include Beasties
end
end
module Beasties
def __parasites__
@sdeming
sdeming / centos5-ruby192-chef.sh
Created February 1, 2012 05:25
Bootstrap ruby 1.9.2 with chef on CentOS 5, needs updating to CentOS 6 and ruby 1.9.3
#!/bin/bash
export GIT_SSL_NO_VERIFY=true
# rpmforge
rpm -Uhv http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS//rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
yum update -y
# dev requirements
yum groupinstall -y "Development Tools"
yum install -y \
@sdeming
sdeming / avos.rb
Created January 31, 2012 21:08
AutoVivifying OpenStruct
module AutoVivifying
def method_missing name, *args
if name.to_s !~ /\=$/
self.send(:"#{name}=", self.class.new)
else
super
end
end
end