Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
@havenwood
havenwood / swcfg.rb
Created Mar 7, 2011
Rubyish switches config
View swcfg.rb
require 'sinatra'
Switch = Struct.new(:vendor, :model, :ports)
Ports = Struct.new(:first, :second)
get '/' do
erb :select_switch
end
post '/' do
View gist:875642
require 'sinatra'
# routes
get '/' do
erb :body_vendor
end
post '/' do
@switch = params[:vendor]
case @switch
View gist:990164
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 Jun 6, 2011
Remove dups from an array in Javascript
View dedupe.js
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);
View gist:1020949
#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
View gist:1020950
#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
View tear-the-gems-down.rb
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}" }
View fizz.rb
class Integer
def check
case
when self % 15 == 0
puts "Fizzbang"
when self % 3 == 0
puts "Fizz"
when self % 5 == 0
puts "Bang"
else
View palindrome.rb
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
View fizzbuzz.rb
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
You can’t perform that action at this time.