Skip to content

Instantly share code, notes, and snippets.

@mudge
mudge / logstash.conf
Last active April 17, 2019 07:58
A grok pattern for Rails 3.2 logs for use with logstash. Assumes that you have a multiline filter to combine Rails logs into one line and only one worker is logging to a file (c.f. https://gist.github.com/mudge/5063930).
multiline {
tags => ["rails"]
pattern => "^Started"
negate => true
what => "previous"
}
@mudge
mudge / months.rb
Created October 8, 2012 13:11
A tail-recursive Ruby method to split a date range into smaller month-based date ranges.
RubyVM::InstructionSequence.compile_option = {
:tailcall_optimization => true,
:trace_instruction => false
}
RubyVM::InstructionSequence.new(<<-RUBY).eval
def acc(start_date, end_date, accu)
if start_date >= end_date
accu + [end_date]
else
@mudge
mudge / gist:203141
Created October 6, 2009 15:50
Pipe input into a system command in Ruby.
result = IO.popen(command, 'r+') do |io|
io.write(input)
io.close_write
io.read
end
@mudge
mudge / dijkstra_with_fibonacci_heap.rb
Last active July 15, 2018 16:14
Implementing Dijkstra's algorithm with Fibonacci Heap
require 'fibonacci_heap'
# Assuming `graph` is an object with the following interface that stores vertices as `FibonacciHeap::Node`
# instances and `source` is a `FibonacciHeap::Node`:
#
# * `graph.vertices`: return an Enumerable of all vertices in the graph
# * `graph.neighbours(u)`: return an Enumerable of all vertices that neighbour a given vertex `u`
# * `graph.length(u, v)`: return the numeric weight of the edge between two given vertices `u` and `v`
def dijkstra(graph, source)
dist = Hash.new(Float::INFINITY)
@mudge
mudge / fibonacci_heap.rb
Last active June 24, 2018 19:56
A Ruby implementation of a Fibonacci heap
class CircularDoublyLinkedList
include Enumerable
class Node
include Comparable
attr_accessor :key, :next, :prev
alias right next
alias left prev
@mudge
mudge / dijkstra.rb
Last active June 22, 2018 15:41
An implementation of Dijkstra's algorithm in Ruby
# A pure Ruby implementation of https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
#
# Takes a graph in hash form, e.g. { 'A' => ['B', 'C'], 'B' => ['D'], 'C' => ['D'] },
# and a source node, e.g. 'A', and returns the distances to every reachable node and a hash of "next hops" for each node.
def dijkstra(graph, source)
q = graph.keys
prev = {}
dist = Hash.new(Float::INFINITY)
dist[source] = 0
@mudge
mudge / snitch.rb
Last active February 17, 2018 13:56
A wrapper class for reporting on method calls useful for reverse engineering
# snitch = Snitch.new('A string')
#=> #<Snitch:0x00007fe53a1964b0 @obj="A string">
# snitch.empty?
# Calling empty? on A string with []
#=> false
class Snitch
attr_reader :obj
def initialize(obj)
@mudge
mudge / gist:163332
Created August 6, 2009 14:19
A Ruby method to validate UK postcodes.
# Validate a UK postcode using a modified version of the official
# regular expression provided by
# http://www.govtalk.gov.uk/gdsc/schemaHtml/bs7666-v2-0-xsd-PostCodeType.htm
#
# @param [String] postcode the postcode to validate
# @return [Boolean] true if the postcode is valid, false if not
def is_valid_postcode?(postcode)
!!(postcode =~ /^\s*((GIR\s*0AA)|((([A-PR-UWYZ][0-9]{1,2})|(([A-PR-UWYZ][A-HK-Y][0-9]{1,2})|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))\s*[0-9][ABD-HJLNP-UW-Z]{2}))\s*$/i)
end
@mudge
mudge / gist:4111082
Created November 19, 2012 14:53
Capistrano task to use relative symlinks instead of absolute.
require "pathname"
namespace :deploy do
task :create_symlink, :except => { :no_release => true } do
deploy_to_pathname = Pathname.new(deploy_to)
on_rollback do
if previous_release
previous_release_pathname = Pathname.new(previous_release)
relative_previous_release = previous_release_pathname.relative_path_from(deploy_to_pathname)