Skip to content

Instantly share code, notes, and snippets.

View sirupsen's full-sized avatar
🐡

Simon Eskildsen sirupsen

🐡
View GitHub Profile
@sirupsen
sirupsen / average.rb
Created August 22, 2009 20:45
Which language is best at taking all the integer paraments and get the average? Look for yourself!
def average(*numbers)
((numbers.inject {|sum, element| sum + element}) / numbers.length).to_f
end
@sirupsen
sirupsen / template.php
Created September 16, 2009 16:30
Templating abstraction in PHP.
<?php
require_once("bootstrap.php");
/*
*
* The template class to handle the output to a template file. It's good
* because it seperates all the PHP code from the actual HTML, except for
* the PHP markup used to output the data.
*
* It's used by making a file in the root directory
@sirupsen
sirupsen / template.php
Created December 2, 2009 14:30
Simple templating engine in PHP.
<?php
/*
*
* Simple template engine
*
*/
class Template {
private $data;
@sirupsen
sirupsen / ws.c
Created January 22, 2010 08:03
Quine in C.
#include <stdio.h>
main() {
// String to contain program
char *program = "#include <stdio.h>%cmain() {%c char *program = %c%s%c;%c printf(program, 10, 10, 34, program, 34, 10, 10, 10);%c}%c";
/* Insert ascii characters for colon and newline
Write out the program, two new lines, at 34 we start colons
at the program pointer, in the colons write out the program
_again_ stop colons, and paste out some new lines, done! */
printf(program, 10, 10, 34, program, 34, 10, 10, 10);
}
@sirupsen
sirupsen / launch-sakura
Created March 23, 2010 13:45
Just a single terminal. Please.
#! /bin/bash
WINTITLE="sakura" # Name of the window (or part of it)
PROGRAMNAME="sakura" # Name of the program, so it can be opened if there's no window currently
# Lists all windows, if there's one containing $WINTITLE it'll return 1, and bring the current instance of the program to the front.
if [ `wmctrl -l | grep -c "$WINTITLE"` != 0 ]
then
wmctrl -a "$WINTITLE"
# Else, it'll launch a new instance
else
@sirupsen
sirupsen / bd.rb
Created May 15, 2010 23:23
Implementation of the "BrainDamage" language.
# Implementation of TheLinx's BrainDamage in Ruby
# => http://gist.github.com/402454
#
class BrainDamage < Array
SPACE = 32
NEWLINE = 10
def run(file)
file = get_file_content(file)
@sirupsen
sirupsen / fizzbuzz.rb
Created December 28, 2010 17:43
Fizzbuzz in Ruby.
# not so insane
class Fixnum
def fizzbuzz
buffer = ''
buffer += 'Fizz' if self % 3 == 0
buffer += 'Buzz' if self % 5 == 0
buffer.empty? ? self : buffer
end
end
@sirupsen
sirupsen / serialization.rb
Created March 7, 2011 15:06
Benchmark of "serialization"
require 'benchmark'
require 'redis'
require 'yaml'
require 'json'
N = 100000
Benchmark.bm do |r|
@redis = Redis.new
@serialize_me = [1,2,3,4,5]
@sirupsen
sirupsen / epenis.rb
Created March 12, 2011 22:46
Ruby implementation of judofyr's codegolf challenge: http://codegolf.stackexchange.com/questions/1570/uptime-bash-penis
#!/usr/bin/env ruby
puts "8#{'='*STDIN.read[/\d/].to_i}D"
# $ uptime
# 23:42 up 56 days, 15:19, 2 users, load averages: 0.34 0.41 0.41
# $ uptime | ruby epenis.rb
# 8========================================================D
@sirupsen
sirupsen / explain.rb
Created December 6, 2011 06:35
.ircrb entry to run explain on a query.
if defined? ActiveRecord
def explain(query)
query = query.to_sql if query.is_a?(ActiveRecord::Relation)
ActiveRecord::Base.connection
.execute("EXPLAIN ANALYZE #{query}")
.to_a
.each { |hash| puts hash["QUERY PLAN"] }
nil