This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double this[string key] { | |
get | |
{ | |
if (InnerHashtable.ContainsKey(key)) | |
{ | |
return (double)InnerHashtable[key] | |
} | |
else | |
{ | |
return 0.0d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- @items && @items.each do |item| | |
- name = item.gsub(/views\/.+\/(.+).haml/, '\1') | |
- unless name == "index" || name[/^hide-/] | |
%p do some stuff |