Skip to content

Instantly share code, notes, and snippets.

@Antti
Antti / gist:1106691
Created July 26, 2011 12:57
Ruby Style Guide
Original Source: https://github.com/chneukirchen/styleguide
= Christian Neukirchen's Ruby Style Guide
You may not like all rules presented here, but they work very well for
me and have helped producing high quality code. Everyone is free to
code however they want, write and follow their own style guides, but
when you contribute to my code, please follow these rules:
@Antti
Antti / config.ru
Created March 6, 2012 12:32
Rails Lightweight Stack. Most of this is detailed on Crafting Rails Applications - http://pragprog.com/book/jvrails/crafting-rails-applications
# Run this file with `RAILS_ENV=production rackup -p 3000 -s thin`
# Be sure to have rails and thin installed.
require "rubygems"
# We are not loading Active Record, nor the Assets Pipeline, etc.
# This could also be in your Gemfile.
gem "actionpack", "~> 3.2"
gem "railties", "~> 3.2"
# The following lines should come as no surprise. Except by
@Antti
Antti / threadpool.rb
Last active October 5, 2015 06:08
Threadpool
class ThreadPool
def initialize(size)
@size = size
@queue = Queue.new
@pool = Array.new(@size) do |i|
Thread.new do
Thread.current.abort_on_exception = true
Thread.current[:id] = i
catch(:exit) do
@Antti
Antti / ntp.rb
Created May 22, 2012 10:06
Simple NTP query
require 'socket'
s = UDPSocket.new
NTP_PACKET_SIZE= 48
EPOCH_SECONDS = 2208988800
ntppacket = [0b11100011, 0, 6, 0xEC, 0, 49, 0x4E, 49, 52,0,0,0,0].pack("C4QC4Q4")
puts "Packet size matches: #{ntppacket.bytes.count == NTP_PACKET_SIZE}"
s.send ntppacket, 0, "pool.ntp.org", 123
response = s.recv(NTP_PACKET_SIZE)
@Antti
Antti / app_config.rb
Created September 25, 2012 14:11
Rails Application Config loader
# gem 'hashie'
begin
app_name = Rails.application.class.parent_name.downcase
file_path = File.join(Rails.root,'config',"#{app_name}.yml")
app_config = Hashie::Mash.new(YAML.load_file(file_path)).freeze
rescue Errno::ENOENT
raise "Can not find #{app_name.upcase} config file: #{file_path}"
rescue Psych::SyntaxError
Rails.logger.error "Can not parse #{app_name.upcase} config file: #{file_path}:"
puts "Can not parse #{app_name.upcase} config file: #{file_path}"
@Antti
Antti / quartile.rb
Created June 13, 2013 12:59
median & quartile
def median_split(arr)
c = arr.size%2
subset = [arr[0..arr.size/2-1], arr[arr.size/2+c..-1]]
m = median arr
subset[0].push m
subset[1].unshift m
subset
end
def median(arr)
@Antti
Antti / camel.pl
Last active December 25, 2015 13:59
#!/usr/bin/perl
$==$';
$;||$.| $|;$_
='*$ ( ^@(%_+&~~;# ~~/.~~
;_);;.);;#) ;~~~~;_,.~~,.* +,./|~
~;_);@-, .;.); ~ ~,./@@-__);@-);~~,.*+,.
/|);;;~~@-~~~~;.~~,. /.);;.,./@~~@-;.;#~~@-;;
;;,.*+,./.);;#;./@,./ |~~~~;#-(@-__@-__&$#%^';$__
='`'&'&';$___="````" |"$[`$["|'`%",';$~=("$___$__-$[``$__"|
"$___"| ("$___$__-$[.%")).("'`"|"'$["|"'#").
require "diff/lcs"
require "diff/lcs/hunk"
require 'pp'
class Differ
def initialize(actual, expected)
@actual = actual
@expected = expected
end
def hunks
@Antti
Antti / alphabet_position.rb
Last active January 1, 2016 05:09
Resolve alphabet position encoded puzzle.
ALPHABET = ('a'..'z').to_a
code = '2114111925'
class WordTree
attr_accessor :l, :r, :my_letter, :string
def initialize(my_letter, string)
@string = string
@my_letter = my_letter
try_init_childs
end
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d