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 / swcfg.rb
Created March 7, 2011 03:59
Rubyish switches config
require 'sinatra'
Switch = Struct.new(:vendor, :model, :ports)
Ports = Struct.new(:first, :second)
get '/' do
erb :select_switch
end
post '/' do
require 'sinatra'
# routes
get '/' do
erb :body_vendor
end
post '/' do
@switch = params[:vendor]
case @switch
require 'readline'
# Empty and dup are BAD.
# 1. Read line and append to history
# 2. Quick Break on nil
# 3. Remove from history if empty or dup
def readline_lineread
line = Readline.readline('> ', true)
return nil if line.nil?
@havenwood
havenwood / dedupe.js
Created June 6, 2011 00:23
Remove dups from an array in Javascript
var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];
var sorted_arr = arr.sort();
var results = [];
for (var i = 0; i < arr.length - 1; i += 1) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
alert(results);
#define a recursive function that will traverse the directory tree
def printAndDescend(pattern)
#we keep track of the directories, to be used in the second, recursive part of this function
directories=[]
Dir['*'].sort.each do |name|
if File.file?(name) and name[pattern]
puts(File.expand_path(name))
elsif File.directory?(name)
directories << name
end
#define a recursive function that will traverse the directory tree
def printAndDescend(pattern)
#we keep track of the directories, to be used in the second, recursive part of this function
directories=[]
Dir['*'].sort.each do |name|
if File.file?(name) and name[pattern]
puts(File.expand_path(name))
elsif File.directory?(name)
directories << name
end
@havenwood
havenwood / tear-the-gems-down.rb
Created June 14, 2011 23:14
Uninstall all gems
open("|gem list").read.each_line { |gem| system "gem uninstall -aI #{gem[/^\w+/]}" }
# or something related like `gem list`.split("\n").each { |gem| system "gem uninstall -aI #{gem.split(" ").first}" }
@havenwood
havenwood / fizz.rb
Created September 12, 2011 21:47
FizzBuzz
class Integer
def check
case
when self % 15 == 0
puts "Fizzbang"
when self % 3 == 0
puts "Fizz"
when self % 5 == 0
puts "Bang"
else
@havenwood
havenwood / palindrome.rb
Created September 12, 2011 21:49
Find Palindromes
def split_up string
@array = string.downcase.split ''
end
def find_palindromes
@palindromes = []
1.upto @array.size do |spot|
look = 1
while @array[spot - look] == @array[spot + look]
@palindromes << @array[spot - look, look * 2 + 1].join
@havenwood
havenwood / fizzbuzz.rb
Created September 13, 2011 02:26
FizzBuzz
1.upto 100 do |n|
r = ""
r << "fizz" if n % 3 == 0
r << "buzz" if n % 5 == 0
r << n.to_s if r.empty?
puts r
end