Skip to content

Instantly share code, notes, and snippets.

@HotFusionMan
HotFusionMan / add_class-level_method_to_module.rb
Created January 14, 2011 15:51
How to add a "class-level" method to an existing module.
module PerfectBitsMath
def number_of_squares_in_interval( m, n )
1
end
end
module Math
extend PerfectBitsMath
end
@HotFusionMan
HotFusionMan / seeds.rb
Created February 28, 2011 23:39
Generic YAML fixture loader for Rails. Supply a db/seeds.yaml file containing fixtures with sections named for their ActiveRecord models, pluralized (e.g., Queries). Invoke with rake db:seed.
# Based on http://stackoverflow.com/questions/2339021/how-can-i-load-some-activerecord-models-from-a-yaml-file-and-save-them-to-the-db/
seed_filepath = File.join( RAILS_ROOT, 'db', 'seeds.yaml' )
config = YAML::load_file( seed_filepath )
config.each { |pluralized_model_name, fixtures|
model_class = pluralized_model_name.singularize.constantize
# If you like leaping without looking:
# model_class.create( fixtures )
@HotFusionMan
HotFusionMan / compare_times_of_day.rb
Created April 13, 2011 21:55
After searching the Web for how to tell whether it's currently later some specified time of day, I realized it's really simple to do.
now = Time.now.to_a
if ( now[2] * 60 + now[1] ) * 60 + now[0] > START_TIME_IN_SECONDS then
# start whatever you wanted to start at this time
end
@HotFusionMan
HotFusionMan / launch_OSX_Login_Items.rb
Created April 20, 2011 18:11
Requires that you've set up a ~/com.apple.loginitems.plist file defining what apps you want to launch. It would be nice to automate converting from using the system's copy of this file to this custom-located one.
#!/usr/bin/env ruby
require 'rexml/document'
def set_up_Growl
if `ps -ax | grep GrowlHelperApp\.app | grep -v grep` == '' then
system( 'open /Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app' )
end
end
@HotFusionMan
HotFusionMan / print_methods_of_a_class.java
Created June 17, 2011 03:26
Using Java reflection to print all the methods of a class, including their argument signatures.
import java.lang.reflect.Method ;
String className = "java.lang.Class" ; // or whatever class you're interested in
Class c = Class.forName( className ) ;
Method[] methods = c.getDeclaredMethods() ;
for ( Method m : methods )
{
@HotFusionMan
HotFusionMan / devops_LFMM.markdown
Created July 12, 2011 16:12
My dev-ops "Learn from my mistakes" list
  • Use a version control system that allows you to deploy exactly what you want to, not "everything up to and including a particular checkin", e.g., use Git, not Perforce.

  • Have a centralized admin user account on servers, but also have individual user accounts and make people log in using their individual and then sudo. This strategy provides a better audit trail than logging in directly to the cetnral admin account.

  • Don't build a job queue system from scratch using cron. There are so many better-behaved job queue systems in various languages.

    • Cron can't schedule more than some maximum number of jobs in a single minute, so it may miss running some jobs.
    • Cron can't re-run missed jobs, so you have to come up with a way yourself of identifying any missed jobs (either because of the above issue or because cron or the system was down) and running them.
  • Databases sometimes need to be versioned in ways besides ActiveRecord-type migrations. For example, if two code branches differ in the columns th

@HotFusionMan
HotFusionMan / installed_jailbreak_packages.txt
Created December 28, 2011 18:48
jailbreak packages currently installed on my iPhone 3GS; follow instructions at http://j.mp/u51urf to install all these packages via command line
actionmenu install
applist install
apr-lib install
apt install
apt7 install
apt7-key install
apt7-lib install
apt7-ssl install
backgrounder install
base install
@HotFusionMan
HotFusionMan / gist:2766541
Created May 22, 2012 04:23
command line for installing mysql gem on 64-bit Mac OX X 10.{6,7}
env ARCHFLAGS="-arch x86_64" gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/usr/local/mysql/lib/ --with-mysql-config=/usr/local/mysql/bin/mysql_config
@HotFusionMan
HotFusionMan / gist:2968572
Created June 21, 2012 21:13
Mac OS X network preference files to delete when Wi-Fi connection does not stay up
cd /Library/Preferences/SystemConfiguration
sudo rm com.apple.airport.preferences.plist com.apple.network.identification.plist
cd ~/Library/Preferences
rm com.apple.internetconfigpriv.plist com.apple.internetconfig.plist
@HotFusionMan
HotFusionMan / gist:2992151
Created June 25, 2012 23:47
run only one Rails test class via Rake
test_type=unit
test_class_name=active_record
rake test:${test_type}s TEST=test/${test_type}/${test_class_name}_test.rb