Skip to content

Instantly share code, notes, and snippets.

@morganp
morganp / ruby_file_operators_i_foget.rb
Created November 22, 2010 09:30
File Operators I forget
File.expand_path( relative_file ) => absolute_file
File.dirname( path/file ) => path
File.basename( path/file ) => file
@morganp
morganp / gist:755577
Created December 26, 2010 19:16
Ruby, set working directory to that of the current file
Dir.chdir( File.dirname( File.expand_path(__FILE__) ) )
@morganp
morganp / int_to_hex_binary_position.rb
Created February 18, 2011 15:41
integer to hex one hot code
def int_to_hex_binary_position( num, chars=8 )
return "0x#{(2**num).to_s(16).rjust(chars,'0')}"
end
int_to_hex_binary_position( 0 )
> 0x00000001
int_to_hex_binary_position( 1 )
> 0x00000002
@morganp
morganp / file_list.rb
Created April 20, 2011 13:11
Ruby list only files
files = (Dir.glob("*.*")+Dir.glob(".*")).delete_if {|k| File.directory?(k)}
@morganp
morganp / addpath.sh
Created May 10, 2011 09:54
Add to bash path if not already there
pre_add_path() {
if [ -s "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
PATH="$1:$PATH"
fi
}
post_add_path() {
if [ -s "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
PATH="$PATH:$1"
fi
}
class Golf
class << self
def hole1 a
a.reduce :*
end
def hole2 s
s.split.sort_by{|i| i[1] }.join ' '
end
class Golf
class << self
def hole1 a
a.reduce :*
end
def hole2 s
s.split.sort_by{|i| i[1] }*' '
end
#!/bin/bash
#VAR is then name of the variable to look for
VAR=$1
eval test=\${$VAR}
if [ "$test" == "" ]
then
require 'pp'
require 'yaml'
require 'active_record'
@environment = ENV['RACK_ENV'] || 'development'
@dbconfig = YAML.load(File.read('config/database.yml'))
ActiveRecord::Base.establish_connection @dbconfig[@environment]
@morganp
morganp / backup.sh
Created July 13, 2011 23:59
Disk level Back up script
#!/bin/bash
# Disk level Back up script
ARCHIVE_SERVER="192.168.0.30"
# Back up drive to target drive on server
disk[0]="/Volumes/Terra03/ $ARCHIVE_SERVER:/mnt/kryten/disk1/tank"
disk[1]="/Volumes/Terra04/ $ARCHIVE_SERVER:/mnt/kryten/disk2/tank"
disk[2]="/Volumes/Terra05/ $ARCHIVE_SERVER:/mnt/kryten/disk3/tank"