Skip to content

Instantly share code, notes, and snippets.

@stephanwehner
stephanwehner / rdb.rb
Last active June 14, 2018 21:24
Alternative to rails dbconsole -- which loads much quicker
#!/usr/bin/ruby
# Instead of rails dbconsole. This loads a lot quicker, since it doesn't load all of rails.
# Place user/password in ~/.my.cnf as
# [client]
# user=whathaveyou
# password=whathaveyoupwd
require 'erb'
# This is for Rails apps
# Place this in app/controllers/shutdown_controller.rb
#
# When you want to start up a new Rails server, but some other one is already running
# simply run this command: "curl http://localhost:3000/shutdown/exit" -- it will go away.
#
# This will work without any other changes if your routes include something like
# match '/:controller(/:action(/:id))', via: [:all]
#
# Also, add app/controllers/shutdown_controller.rb to your global .gitignore file and noone will ever know!
# Simple Key-Value store in Ruby
# Stephan Wehner, May 2017
#
# 2 sample sessions illustrate usage:
# In one terminal:
# $ ruby simple-key-value-store.rb
# In another terminal session:
#
# $ telnet localhost 3333
# Trying ::1...
@stephanwehner
stephanwehner / check_super_permutation_6.rb
Last active August 29, 2015 14:05
Verifying a superpermutation for 6 symbols
# See the Aug. 2014 paper Tackling the Minimal Superpermutation Problem by Robin Houston at http://arxiv.org/abs/1408.5108
# It announces a superpermutation for 6 symbols with length 872
# This Ruby script verifies it is a superpermutation and finds that all permutations only occur once.
# Kind of complements http://arxiv.org/src/1408.5108v1/anc/splitsuperperm.py ("Take a superpermutation, and print it as a sequence of permutations.")
# Invocation:
# ruby check_super_permutation_6.rb
# (Runs in 0.05 secs on a standard laptop, no optimizations necessary)
# Could also read this from http://arxiv.org/src/1408.5108v1/anc/superperm-6-866.txt
SUPER_PERMUTATION =<<END_PERM.gsub("\n",'')
# JSON hashes of course allow multiple values for the same key.
# Only one is kept
# E.g.
# > h = JSON.parse '{"a":1, "a":2}'
# => {"a"=>2}
#
# If you don't like that, you can use this :
require 'json'
class DuplicateEntryCatchingHash < Hash
@stephanwehner
stephanwehner / generate-xkcd-color-survey-name-page.rb
Created July 11, 2014 17:39
Generate HTML page showing the color names from the xkcd color survey
#
# Suppose the file mainsurvey_sqldump.txt (available from colorsurvey.tar.gz, link at http://blog.xkcd.com/2010/05/03/color-survey-results/) has been loaded into file mainsurvey_sqldump.sqlite3, possibly with the command
#
# sqlite3 mainsurvey_sqldump.sqlite3 < mainsurvey_sqldump.txt
#
# Invocation of this Ruby script (UNIX command line)
#
# echo 'select r,g,b,colorname from answers;' | mainsurvey_sqldump.sqlite3 | ruby THIS-FILE > result.html
omit_colors = %w( grey gray yellow red green blue purple brown pink black violet white orange cyan gray ).sort.uniq
# Avoiding heredoc.chomp as in
# assert_select "div#order p", <<-STR.chomp
# Test ABC
# Test DEF
# STR
def assert_select(a,b)
puts "assert_select: #{a.inspect}, #{b.inspect}"
end
@stephanwehner
stephanwehner / ridiculous_adding.rb
Last active August 29, 2015 14:00
Ridiculous Adding
# Add two numbers in Ruby.
# Strategy: make a list of known sums, and look up the one in question.
# What's the point? The point is: nowadays computing machinery is so powerful that
# implementation in many cases doesn't matter. The most ridiculous approach can still be
# useful, and improving it, a waste of time.
# So here's a very silly way how to add numbers.
def add_by_grep(a,b)
@stephanwehner
stephanwehner / notice-google-search
Created March 9, 2012 22:47
Notice / Google Search