Skip to content

Instantly share code, notes, and snippets.

@rennex
rennex / foo.c
Created July 18, 2014 22:52
silly pointer math experiments
#include <stdio.h>
int main(void) {
long x = ""-"";
char *y = "foo";
char *z = "foo";
printf("null is %p, x is %ld\n", NULL, x);
printf("y is %p, z is %p\n", y, z);
return 0;
}
@rennex
rennex / asterisk
Created August 15, 2014 02:54
playing with the * prefix in ruby
[1] pry(main)> x = (1..5)
=> 1..5
[2] pry(main)> p x
1..5
=> 1..5
[3] pry(main)> p *x
1
2
3
4
@rennex
rennex / chooser_middleware.rb
Created August 24, 2014 18:26
Rack middleware that sends requests to different Sinatra apps based on some criteria (randomly in this example).
require "sinatra/base"
class Foo < Sinatra::Base
get "/" do
"foo!"
end
end
class Bar < Sinatra::Base
@rennex
rennex / sequel db skeleton
Last active August 29, 2015 14:15
DB setup skeleton for Sequel
The files shown below let you define the location of the database in one place,
and easily run migrations, the main web app, and any backend utility scripts.
# do this in the web app or utility script:
require_relative "database.rb"
# to run migrations (in the shell):
$ ./migrate
require "sinatra"
set :show_exceptions, :after_handler
error do
"Custom error page!"
end
get "/" do
"Hello! <a href='error'>Click to get an error</a>"
@rennex
rennex / Verkkouutiset title-killer
Created September 8, 2015 12:34
Poistaa Verkkouutisten artikkeleista vitun ärsyttävän title-attribuutin, joka pomppaa esiin aina kun hiiri pysähtyy väärään paikkaan
// ==UserScript==
// @name Verkkouutiset title-killer
// @namespace Rennex
// @include http://www.verkkouutiset.fi/*
// @version 1
// @grant none
// ==/UserScript==
document.querySelector("article").removeAttribute("title")
require "sinatra"
get "/*" do
content_type "text/plain"
ret = ""
env.each_key do |k|
ret += "%25s = %s\n" % [k, env[k]]
end
ret
end
@rennex
rennex / foo.service
Last active July 18, 2023 11:34
Example of a systemd service file to start a Sinatra app.
# /etc/systemd/system/foo.service
[Unit]
Description=Foo service
Wants=nginx.service
Requires=postgresql.service
After=postgresql.service
[Service]
Type=simple
User=foo
@rennex
rennex / example_run.txt
Last active January 25, 2018 02:41
Nondeterministic Ruby behaviour (both with Ruby 1.9.3p392 on a physical machine and with Ruby 2.3.1p112 on a VirtualBox VM)
$ for i in `seq 200`; do ruby puzzle6_2_debug.rb ; done | uniq
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [13, 12, 12, 10, 10, 8, 7, 6, 6, 4, 4, 2, 2, 1, 15, 0]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [6, 6, 4, 2, 2, 0, 0, 15, 13, 12, 11, 11, 9, 8, 4, 9]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [2, 1, 0, 15, 13, 12, 11, 11, 9, 9, 7, 7, 5, 5, 4, 1]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [8, 7, 7, 5, 5, 3, 2, 1, 1, 0, 14, 14, 12, 12, 10, 11]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
@rennex
rennex / rodaplugin_redirect_to_slash.rb
Created February 1, 2018 00:44
Roda plugin to easily support (and prefer) trailing slashes in certain paths.
# frozen-string-literal: true
class Roda
module RodaPlugins
# Treats any string matchers with a trailing slash ("/") almost like
# the slash wasn't there, but GET requests to that path without the
# slash are redirected to the path with a trailing slash.
#
# Example:
#