Skip to content

Instantly share code, notes, and snippets.

View sirupsen's full-sized avatar
🐡

Simon Eskildsen sirupsen

🐡
View GitHub Profile
@sirupsen
sirupsen / attr.rb
Created June 12, 2010 13:12
Explanation of attr_* in Ruby.
# Explanation of attr_* to Noxn
# Let's start with an example class using attr_acessor:
class Klass
attr_acessor :variable
def initialize(content)
@variable = content
end
end
@sirupsen
sirupsen / rc.xml
Created August 9, 2010 08:35
Openbox config (~/.config/rc.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!-- Do not edit this file, it will be overwritten on install.
Copy the file to $HOME/.config/openbox/ instead. -->
<openbox_config xmlns="http://openbox.org/3.4/rc">
<resistance>
<strength>20</strength>
<screen_edge_strength>30</screen_edge_strength>
</resistance>
<focus>
<focusNew>yes</focusNew>
@sirupsen
sirupsen / README.md
Created October 15, 2010 15:23
Simple evented ChatServer in pure Ruby using network I/O with select(2).

Dependencies

Colored for chat colors

gem install colored

It has many comments since I tried to explain what is going on for less-experienced Ruby people.

@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 / pvpn
Created April 14, 2011 14:45
Launches shuttle on untrusted networks.
#!/bin/bash
#
# Launches sshuttle if you're on an untrusted network, currently only available on OS X.
# Patches for Linux support welcome!
#
# sshutle: Transparent proxy server that works as a poor # man's VPN.
# Forwards over ssh. Doesn't require admin. Works with Linux and MacOS. Supports DNS tunneling.
# https://github.com/apenwarr/sshuttle
#
# Dependencies
@sirupsen
sirupsen / vim7.3_mac_install.rb
Created July 27, 2011 21:57 — forked from pengwynn/vim7.3_mac_install.rb
Script to install Vim 7.3 with ruby support for Mac OS X Lion
# requires root permissions in /usr/bin/
star = String.new
8.times { star += "*" }
Star = "\n#{star * 3}\n"
def newblock string
puts "\n#{Star}#{string}#{Star}\n"
end
@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
@sirupsen
sirupsen / graph.rb
Created January 10, 2012 07:08
Find distance from one Twitter profile to another.
require "net/https"
require "uri"
require 'json'
def request(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE