Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
@havenwood
havenwood / merge_sort.rb
Created August 30, 2014 02:42
Merge Sort
def merge_sort array
return array if array.empty? or array.one?
sorted = []
left, right = array.each_slice(array.size.fdiv(2).round).map { |a| merge_sort a }
loop do
break sorted.concat(right) if left.empty?
break sorted.concat(left) if right.empty?
@havenwood
havenwood / cheating.rb
Created August 30, 2014 23:28
Klass Module
module Klass
def self.new *args, &block
object = Class.allocate
object.send :initialize, *args, &block
object
end
end
Cat = Klass.new do
attr_reader :name, :age
@havenwood
havenwood / fizzducer.rb
Last active August 29, 2015 14:07
Transducer FizzBuzz
require 'transducers'
T = Transducers
fizzducer = T.compose T.map { |n| n % 3 == 0 && n % 5 == 0 && 'FizzBuzz' || n },
T.map { |n| n % 3 == 0 && 'Fizz' || n },
T.map { |n| n % 5 == 0 && 'Buzz' || n },
T.map(:to_s)
T.transduce fizzducer, :puts, STDOUT, 1.upto(5)
@havenwood
havenwood / proc_compose.rb
Last active August 29, 2015 14:14
Composing with Procs
class Proc
class << self
def compose *others
others.map(&:to_proc).inject :*
end
end
def * other
proc { |*args| other.call self.call *args }
end
@havenwood
havenwood / roshambo.rb
Created February 10, 2015 18:04
Example of Comparable with Roshambo
class Roshambo
OPTIONS = [:rock, :paper, :scissors]
WINNERS = OPTIONS.rotate.zip(OPTIONS).to_h
LOSERS = WINNERS.invert
attr_reader :value
include Comparable
def initialize value = OPTIONS.sample
@havenwood
havenwood / trust.rb
Created February 16, 2015 23:29
Install a trust certificate to fix `SSL_connect returned=1 errno=0` for old RubyGems
require 'open-uri'
URL = 'https://raw.githubusercontent.com/rubygems/rubygems/master/lib/rubygems/ssl_certs/'
FILENAME = 'AddTrustExternalCARoot-2048.pem'
DIR = File.join RbConfig::CONFIG['rubylibdir'], 'rubygems', 'ssl_certs'
pem = open File.join(URL, FILENAME), &:read
File.write File.join(DIR, FILENAME), pem
@havenwood
havenwood / shell_example.rb
Created March 17, 2015 23:50
An example of Ruby stdlib's Shell
require 'shell'
Shell.verbose = false
Shell.def_system_command 'ls'
Shell.def_system_command 'grep'
sh = Shell.cd Dir.home
sh.transact do
ls('-a') | grep('^\.') > STDOUT
end
@havenwood
havenwood / streemlike_fizzbuzz.rb
Last active August 29, 2015 14:17
The Streem FizzBuzz example in Ruby for fun: https://github.com/matz/streem#examples
require 'shell'
Shell.verbose = false
Shell.def_system_command 'seq'
Shell.new.transact do
echo(
seq('100').map(&:to_i).map { |x|
if x % 15 == 0
"FizzBuzz"
@havenwood
havenwood / keybase.md
Created April 2, 2015 05:24
Keybase proof

Keybase proof

I hereby claim:

  • I am havenwood on github.
  • I am havenwood (https://keybase.io/havenwood) on keybase.
  • I have a public key whose fingerprint is 06CB 6789 306C 1DC4 BEAE ABF1 CF61 5467 50DB C4B4

To claim this, I am signing this object:

@havenwood
havenwood / install_pkgsrc.sh
Last active August 29, 2015 14:19
A pkgsrc 2015Q2 install script for OS X
function install_pkgsrc() {
local pkgsrc_url="http://pkgsrc.joyent.com/packages/Darwin/bootstrap"
local pkgsrc_archive="bootstrap-2015Q2-x86_64.tar.gz"
local pkgsrc_sha256sum="5e9717932454c6c06db2004a0efe59b40a216aa2c661bc323bd651f8ce4e6c46"
local pkgsrc_gpgkey="0xDE817B8E"
echo ">>> Downloading pkgsrc archive for OS X ..."
\curl -O --progress-bar "$pkgsrc_url/$pkgsrc_archive" || return $?
echo ">>> Verifying pkgsrc archive SHA256 checksum ..."