Skip to content

Instantly share code, notes, and snippets.

require 'benchmark'
def benchmark(setting, &block)
data = (1..10_000_000).map { block.call }
Benchmark.bm(15) do |x|
x.report("compact #{setting}") do
data.dup.each do |d|
[d[0],d[1]].compact.min
end
@benmanns
benmanns / redirect.html
Created April 19, 2011 14:42
Redirect demo for Mechanize meta refresh issue.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Redirect</title>
<meta name="author" content="Benjamin Manns" />
<meta http-equiv="refresh" content="10;url=http://mechanize.rubyforge.org/mechanize/" />
</head>
<body>
<div>
# Basic Setup:
class User
has_one :widget, :dependent => :destroy
end
class Widget
belongs_to :user
end
BIG_HASH_OF_OFTEN_UNCHANGING_VALUES = { ... }
BIG_HASH_OF_A_FEW_CHANGED_VALUES = { ... }
# Solve for x:
BIG_HASH_OF_OFTEN_UNCHANGING_VALUES.merge(x) == BIG_HASH_OF_A_FEW_CHANGED_VALUES + y
# where y is a set of k => v pairs that are in UNCHANGING but not in A_FEW_CHANGED
x = {}
BIG_HASH_OF_A_FEW_CHANGED_VALUES.each do |k, v|
x[k] = v unless v == BIG_HASH_OF_OFTEN_UNCHANGING_VALUES[k]
@benmanns
benmanns / gist:1267915
Created October 6, 2011 16:49 — forked from andrewpthorp/gist:1267848
Preference for helper specs?
# Option 1:
# Use content to separate the helper from the spec
it "should have something" do
content = helper.some_helper_method
content.should have_tag('a', :text => 'foo')
end
# Option 2:
# Combine helper call and spec
@benmanns
benmanns / symmetric.rb
Created October 18, 2011 15:57
Provides symmetric encryption from abc.txt to abc.encrypted and abc.encrypted to abc.txt.
# See http://ruby-doc.org/stdlib-1.9.2/libdoc/digest/rdoc/Digest/Class.html#method-c-digest
# See http://ruby-doc.org/stdlib-1.9.2/libdoc/openssl/rdoc/OpenSSL/Cipher.html
# See http://ruby.about.com/od/rubyfeatures/a/argv.htm
require 'openssl'
# Use this section to encrypt a file
input_file = 'abc.txt'
output_file = 'abc.encrypted'
@benmanns
benmanns / config-s3.yml
Created December 7, 2011 20:15
rake task to migrate paperclip attachments to Amazon S3
# Required:
access_key_id:
secret_access_key:
bucket_name:
# Optional:
server: s3.amazonaws.com
port: 80
use_ssl: false
persistent: false

CSCI 466 Project 4

Benjamin Manns

Part 1

  • Number of Components: 5
  • Average tail length: 105
  • Maximum tail length: 257
  • Minimum cycle length: 4
// Run this in the JavaScript console to remove the SOPA message.
//
// Also, slap yourself for waiting 'til the last minute to finish the report.
//
// I mean, come on.
//
// Seriously.
$('div#mw-sopaOverlay').remove();
$('body').children().show();

First things first, you want to make sure that all of your iptables chains are set to ACCEPT, otherwise, you might lock yourself out of the system by blocking the SSH port.

sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT

Next, you can flush your chains to start from scratch.