Skip to content

Instantly share code, notes, and snippets.

View Narnach's full-sized avatar

Wes Oldenbeuving Narnach

View GitHub Profile
# Ruby 1.8.6p111
# ruby1.9 is 1.9.1 from macports
# jruby latest head from github
# macruby is 0.3.1 from the .zip on their website
# rubinius is latest head from github
narnach@narnachbook ~/Development $ ruby speed.rb && ruby1.9 speed.rb && jruby speed.rb && macruby speed.rb && rbx speed.rb
user system total real
define_method :one 0.990000 0.000000 0.990000 ( 1.001891)
define_method :two 0.980000 0.010000 0.990000 ( 1.000110)
@Narnach
Narnach / genpassword.rb
Created January 8, 2009 12:15
Generate a random alphanumeric password
#!/usr/bin/env ruby
# Random alphanumeric password generator
# Takes one parameter, which is the password length
# Forces the minimum length to be 10 or more
#
CHARS = (0..9).to_a + ('a'..'z').to_a + ('A'..'Z').to_a
class << CHARS
def rand
self[Kernel.rand(size)]
end
@Narnach
Narnach / cotd.rb
Created January 12, 2009 15:07
Show the manpage of a random executable in your $PATH
#!/usr/bin/env ruby
# Command of the day
# Picks a random executable from your path until it finds one with a manpage.
# Use this to learn about some of the really obscure yet really useful scripts that are already on your system.
files = ENV["PATH"].split(":").uniq.compact.map { |path|
Dir.glob(File.join(path,'*')).select {|file| File.file?(file) && File.executable?(file)}
}.flatten.compact.uniq
cotd = File.basename(files[rand(files.size)])
#!/usr/bin/env ruby
require 'rubygems'
require 'active_support'
class John
end
j_c = John.class_eval do
def hi
'hi'
@Narnach
Narnach / build_gems.rb
Created January 14, 2009 09:12
Build all versions of a rubygem based on git tags for each version
#!/usr/bin/env ruby
# BuildGems
# Created by Wes Oldenbeuving, copyright 2009, licensed under MIT LICENSE.
#
# For a git project, build the gems for all gem tag releases.
# The assumptions made are the following:
# - Version tags have the format 'N.N', 'N.N.N', etc... Only digits and dots.
# - There is a 'rake gem' task that builds the gem for the checked out version
# - Gems are located in pkg/*.gem
# - You want to copy all created gems to ~/gems/ after creating them.
#!/bin/sh
# Usage: s2b.sh branchname
# Easily switch between branches and their databases for Rails projects.
# Checks out given branch and copies the branch's database.yml to the active database.yml
# Outputs a small list of databases being used.
# Make sure you have set up your branch's database.yml in #{RAILS_ROOT}/config/database.#{branchname}.yml
git checkout $1 && cp config/database.$1.yml config/database.yml
databases=`grep --colour=auto database config/database.yml | grep -v ^# | sed s/database\:// | tr -d '\012' | sed -E s/^\ +\|\ +$// | sed -E s/\ +/,\ /g`
echo "Using databases: $databases"
@Narnach
Narnach / gist:51081
Created January 23, 2009 17:05
Remove duplicates from your $PATH environment variable
#!/usr/bin/env ruby
# cleanpath - Cleanup your $PATH!
#
# Reads the current $PATH and outputs a version without duplicate entries.
# It is useful as a last line in ~/.profile:
# export PATH=`/path/to/cleanpath`
path = ENV['PATH']
clean_path = path.split(":").uniq.join(":")
puts clean_path
s1 = {:host=>'foo', :user => 'foobar'}
s2 = {:host=>'bar', :user => 'foobar'}
s3 = {:host=>'baz', :user => 'foobar'}
s4 = {:host=>'moo', :user => 'foobar', :options => {:password => 'barfoo'}}
def gate(s, s2, opts={})
gw = Net::SSH::Gateway.new(s[:host], s[:user], {:port => 22}.merge(opts))
gw_port = gw.open(s2[:host], s2[:port])
yield(gw_port)
ensure
require 'rubygems'
require 'net/ssh/gateway'
s1 = {:host=>'foo', :user => 'foobar', :options => {:password => 'barfoo', :port => 2221}}
s2 = {:host=>'bar', :user => 'foobar', :options => {:password => 'barfoo', :port => 2222}}
s3 = {:host=>'baz', :user => 'foobar', :options => {:password => 'barfoo', :port => 2223}}
s4 = {:host=>'moo', :user => 'foobar', :options => {:password => 'barfoo', :port => 2224}}
gw = Net::SSH::Gateway.new(s[:host], s[:user], s[:options)
gw_port = gw.open(s2[:host], s2[:port])
gw2 = Net::SSH::Gateway.new(s2[:host], s2[:user], s[:options].merge({:port => gw_port}))
@Narnach
Narnach / eachfile.rb
Created January 29, 2009 15:11
Execute a command for each of the provided files, or to all files in the current directory
#!/usr/bin/env ruby
# eachfile.rb - Execute a command for each file
def help
puts <<-HELP
Syntax: #{$0} [option] "COMMAND" File1 [File2] .. [FileN]
The command is executed for each of the specified files.
FILE in the command is substituted with the name of each file.
If there is no FILE in command, the filename is given as parameter instead.
When no files are provided, all files in the current directory are used.