Skip to content

Instantly share code, notes, and snippets.

View joho's full-sized avatar
At harbour

John Barton joho

At harbour
View GitHub Profile
@joho
joho / gist:47746
Created January 16, 2009 00:56 — forked from lightningdb/gist:47739
# what is the idiomatic ruby for this:
# if the var is an array, do_something with each element, otherwise just do something with the var
# if var.is_a?(Array)
# var.each do |element|
# do_something(element)
# end
# else
# do_something(var)
# end
@joho
joho / lazy_loader_problems.rb
Created January 22, 2009 05:02
A small problem with my lazy loader
# http://github.com/joho/lazy-loader
# discovered a small problem when I lazy load something that returns nil
# lazy_load in this case is proxying nil
# so ideally a would behave the same as a = nil
a = lazy_load { nil }
# this is fine
nil.nil? # => true
a.nil? # => true
@joho
joho / gist:50822
Created January 23, 2009 00:56 — forked from ryan-allen/gist:50818
# I don't tend to like writing this kind of code:
if Rails.env == 'development' or Rails.env == 'staging'
# ...
end
# So, an alternative Ruby way to do this is:
if %w(development staging).include?(Rails.env)
# ...
@joho
joho / deploy.rb
Created January 27, 2009 04:02
cheap and easy way to manage crontab with cap
# the simplest thing that could work...
desc 'install crontab from config/crontab.txt'
task :install_crontab, :roles => :cron_runner, :except => { :no_release => true, :no_symlink => true } do
run "cd #{current_release}; crontab config/crontab.txt"
end
@joho
joho / gist:65468
Created February 16, 2009 23:55
Proper english method calls
class Object
def method_missing(meth, *args, &block)
proper_english = { 'colour' => 'color',
'pluralise' => 'pluralize',
'metre' => 'meter' }
american_method_name = proper_english[meth.to_s]
if american_method_name
puts "** Using the proper english to call method #{american_method_name}"
return self.send(american_method_name, *args, &block)
@joho
joho / specs.rake
Created February 16, 2009 23:57
Quickly add empty specs for all your models
desc "create missing rspecs for all models"
task :create_missing_model_specs do
models = Dir.open("#{RAILS_ROOT}/app/models").collect {|file_name| file_name.split('.')[0] }
models.each do |model|
if model && model.size > 0
spec_path = "#{RAILS_ROOT}/spec/models/#{model}_spec.rb"
unless FileTest::exist?(spec_path)
puts "creating missing #{model} spec"
File.open(spec_path, "w") do |f|
f.puts "require File.dirname(__FILE__) + '/../spec_helper'"
@joho
joho / gist:65476
Created February 16, 2009 23:59
Slow crappy implementation of a sparse vector in c#
public double this[string key] {
get
{
if (InnerHashtable.ContainsKey(key))
{
return (double)InnerHashtable[key]
}
else
{
return 0.0d
@joho
joho / post-commit.rb
Created February 17, 2009 00:01
SVN post commit diff email script in ruby
#!/usr/bin/ruby -w
# Send svn checkin email, based on a script by Adam Doppelt,
# in turn based on a script by Elliott Hughes.
# Paste this into a file called "post-commit" in your repo's hooks
# folder
# jb - http://whoisjohnbarton.com
require 'cgi'
require 'net/smtp'
@joho
joho / gist:65480
Created February 17, 2009 00:04
shitty helper method for cleaning up multi model's error messages (rails 1.2 blergh)
def tidy_error_messages(model, included_model_names)
new_errors = ActiveRecord::Errors.new(nil)
model.errors.each do |key, message|
new_errors.add(key, message) unless included_model_names.include? key
end
# re-jig the errors for the included models
for included_model_name in included_model_names
for included_model in model.send(included_model_name)
@joho
joho / gist:76717
Created March 10, 2009 03:55 — forked from lachie/gist:76701
- @items && @items.each do |item|
- name = item.gsub(/views\/.+\/(.+).haml/, '\1')
- unless name == "index" || name[/^hide-/]
%p do some stuff