Skip to content

Instantly share code, notes, and snippets.

View pricees's full-sized avatar

Ted Price pricees

View GitHub Profile
@pricees
pricees / Reliability vs. Downtime
Created February 24, 2011 19:57
Percentage of reliability (99% uptime) and the amount of downtime it relates to:
Nines of Reliability: (Hours / Minutes / Seconds)
2 9's (99%) = up to 87.6h / 5256.0m / 315360.0 seconds of downtime per year.
3 9's (99.9%) = up to 8.76h / 525.6m / 31536.0 seconds of downtime per year.
4 9's (99.99%) = up to 0.876h / 52.559999999999995m / 3153.6 seconds of downtime per year.
5 9's (99.999%) = up to 0.0876h / 5.256m / 315.36 seconds of downtime per year.
6 9's (99.9999%) = up to 0.00876h / 0.5256000000000001m / 31.536 seconds of downtime per year.
7 9's (99.99999%) = up to 8.76E-4h / 0.05256m / 3.1536 seconds of downtime per year.
@pricees
pricees / gist:3088839
Created July 11, 2012 07:57
Gist a gist
class Person
attr_accessor :name
end
@pricees
pricees / gist:3088857
Created July 11, 2012 08:04
"Counter Hash"
#########################################
#
# ==Keeps count of number of times key has been added.==
#
# Use:
# hsh = Hash.counter_hsh
# hsh << :foo
# hsh << :foo << :foo
# hsh << "foo"
#
moby = <<EOL
Call me Ishmael. Some years ago - never mind how long precisely - having little
or no money in my purse, and nothing particular to interest me on shore, I
thought I would sail about a little and see the watery part of the world.
[Moby Dick - 1st paragraph]
EOL
#
# "." - matches any character
# "\s" - matches digits (0-9)
@pricees
pricees / gist:3103091
Created July 13, 2012 06:20
The curious behaviors of public, private and protected methods in Ruby
#
# The interesting world of public, private, and protected.
#
# Publics - Work as expected
#
# Privates - Work as expected, except:
# You can send a symbol of the private method as an end around
# Classes can call the privates of their superclasses
#
# Protected - Instances of the same class can call each others protecteds, whut?!
@pricees
pricees / gist:3106997
Created July 13, 2012 19:50
Quick demo of overloading unary, binary operators.
############################
#
# The world is your oyster. Unless you overload it.
# Then it can be anything your heart desires.
#
# Very few operators can't be overloaded: and, or, not, &&, ||
#
class Person
attr_reader :my_bag
@pricees
pricees / gist:3132488
Created July 17, 2012 22:11
Simple Benchmarking Ruby Script
#! /usr/bin/env ruby
require 'benchmark'
#
# Benchmarking cmd-line tool
#
#
# $ bench_it.rb "x = 5; 100.times { x * 5 }" "(1..1000).to_a.inject(:+)" --times=500
#
# EXAMPLE OUTPUT
@pricees
pricees / gist:3168338
Created July 24, 2012 06:17
The Counting Sort
module Algoruby
module Sort
#
# === Counting Sort ===
#
# Best: O(n) [linear]
# Avg: O(n) [linear]
# Worst: O(n) [linear]
#
module Counting
@pricees
pricees / gist:3168496
Created July 24, 2012 07:03
Module Fun and Pythony Methods In Ruby
module Greeting
extend self
def say_hello(greetable = self)
puts greetable.greeting
end
end
class Person
attr_accessor :greeting
@pricees
pricees / gist:3173114
Created July 24, 2012 22:32
Hash#symbolize_keys/symboilze_keys!
class Hash
def symbolize_keys
inject(Hash.new) do |result, (k, v)|
key = k.is_a?(String) ? k.to_sym : k
val = v.is_a?(Hash) ? v.symbolize_keys : v
result[key] = val
result
end
end