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 / genes.rb
Created July 19, 2012 18:42 — forked from Poincare/gist:3055730
refactored genetic
class Chromosome
attr_accessor :bit_string, :fitness
def initialize length, bit_string = nil
@length = length
if bit_string.nil?
generate_random length
else
@bit_string = bit_string
@havenwood
havenwood / atoi.rb
Created November 14, 2012 19:26 — forked from stcatz/atoi.rb
A simple atoi function in ruby.
# A simple translate from a string to integer.
# A minus before all numbers will be consider to be a negative number.
def atoi(str)
return str if !str.is_a? String
fixed_str = fix(str)
result = 0
@havenwood
havenwood / atoi_refined.rb
Created November 14, 2012 19:26 — forked from stcatz/atoi.rb
A simple atoi function using ruby 2.0 refinements.
module AtoiString
refine String do
def atoi
fixed_self = fix self
result = 0
fixed_self.each_char do |c|
result = result * 10 + c.to_i
end

tmux cheatsheet

As configured in my dotfiles.

start new:

tmux

start new with session name:

@havenwood
havenwood / gist:4566945
Last active December 11, 2015 07:29
IRC gyre007
require 'json'
test2 = {
:foo1 => {
:target => "Something1",
:alias => "some_alias1"
},
:foo2 => {
:target => "Something2",
:alias => "some_alias2"
require "./alfred"
results = Alfred::Feedback.new
available_outputs = `./SwitchAudioSource -a | grep "(output)" | sed 's/ (output)$//'`
current_output = `./SwitchAudioSource -c`
def ditch_line available, current
available.split("\n").delete_if { |line| line == current }.join("\n")
end
array = (1..12).map &:to_s
class Array
def insert_every n, this
each_slice(n).inject { |r, a| r << this << a }.flatten
end
end
array.insert_every 3, ','
#=> ["1", "2", "3", ",", "4", "5", "6", ",", "7", "8", "9", ",", "10", "11", "12"]

Ruby Number Conversion Chart

From To Expression
@havenwood
havenwood / gist:5605341
Last active December 17, 2015 11:49 — forked from anonymous/gist:5605311
def fib n
a, b = 1, 2
sum = 0
while a < n
sum += a if a.even?
a, b = b, a + b
end
sum
end
@havenwood
havenwood / learn.rb
Last active March 16, 2016 11:15 — forked from tylerneylon/learn.lua
Learn Ruby: A fork of Learn Lua (https://gist.github.com/tylerneylon/5853042)
# An octothorp (#) starts a single-line comment.
=begin
Starting with a =being and ending with a =end
makes it a multi-line comment.
=end
##
# 1. Variables and flow control.
#