Skip to content

Instantly share code, notes, and snippets.

View simonbaird's full-sized avatar
💭
🤔

Simon Baird simonbaird

💭
🤔
View GitHub Profile
@simonbaird
simonbaird / use_helper_method.rb
Created October 3, 2011 00:15
Pick and choose helper methods to use in your controller actions
#
# If you want to use helper methods inside your controller's actions
# (even though it is arguably a code smell, ie you should push your
# presentation logic down into views rather than have it in your
# controllers), you can include the helper modules you want, for
# example:
#
# class ThingController < ActionController::Base
# include ApplicationHelper
# include ThingHelper
@simonbaird
simonbaird / gist:1228368
Created September 20, 2011 04:59
html_safe gotcha
#
# This is pretty confusing until you know what's going on...
#
# See http://techspry.com/ruby_and_rails/html_safe-and-helpers-in-rails-3-mystery-solved/
# for an explanation...
#
ruby-1.8.7-p334 :025 > foo_safe = "<p>foo</p>".html_safe
=> "<p>foo</p>"
ruby-1.8.7-p334 :026 > (foo_safe + "<p>bar</p>").html_safe # not what you thought! unintuitive!
# See http://tauday.com/
module Math
TAU = PI * 2.0
end
if defined? BigMath
module BigMath
module_function
def TAU(prec)
@simonbaird
simonbaird / tau.patch
Created June 16, 2011 15:51
Patch ruby to define tau
diff --git a/ext/bigdecimal/lib/bigdecimal/math.rb b/ext/bigdecimal/lib/bigdecimal/math.rb
index c17841f..c672cbf 100644
--- a/ext/bigdecimal/lib/bigdecimal/math.rb
+++ b/ext/bigdecimal/lib/bigdecimal/math.rb
@@ -9,6 +9,7 @@ require 'bigdecimal'
# atan(x, prec) Note: |x|<1, x=0.9999 may not converge.
# exp (x, prec)
# log (x, prec)
+# TAU (prec)
# PI (prec)
@simonbaird
simonbaird / dbdump.sh
Created March 17, 2011 00:26
Some bash functions to dump and load a mysql database
# Dump a mysql database to a gzipped sql file
dbdump() {
# (adjust defaults as required)
GZ_FILE=$1; [ ! -n "$GZ_FILE" ] && GZ_FILE="$HOME/.dbdump.gz"
DB_NAME=$2; [ ! -n "$DB_NAME" ] && DB_NAME='fms'
echo -n "Dumping database $DB_NAME to $GZ_FILE..."
# Add --extended-insert=FALSE to mysql command for more readable but MUCH slower sql
mysqldump -u root $DB_NAME | gzip - > "$GZ_FILE"
echo ' Done'
}
@simonbaird
simonbaird / on_the_fly_module_include.rb
Created February 24, 2011 05:29
test including a module on the fly using the singleton class (probably a bad idea...)
# Where the magic happens...
module RuntimeInclude
def get_singleton
class << self
self
end
end
def runtime_include(the_module)
get_singleton.send(:include,the_module)
class Date
[:beginning_of_fortnight, :end_of_fortnight, :next_fortnight].each do |method|
define_method(method) do |*args|
self.to_time.send(method,*args).to_date
end
end
end
class Date
# Going to be lazy here...
[:beginning_of_fortnight, :end_of_fortnight, :next_fortnight].each do |method|
define_method(method) do |*args|
self.to_time.send(method,*args).to_date
end
end
end
@simonbaird
simonbaird / hamrbl.rb
Created November 17, 2010 10:52
write ruby with python style whitespace (a haml hack)
require 'rubygems'
require 'haml'
# Put a - char on each line in just the right place.
# (Need to remove blank lines too, they cause problems)
haml_source = File.read($0).gsub(/\n+/,"\n").gsub(/^\s*/,'\&-')
begin
# Run the file with haml
Haml::Engine.new(haml_source).render
rescue Exception => e
# Spit out the haml source to make it easier to find your error
@simonbaird
simonbaird / include_and_extend_experiments.rb
Created November 17, 2010 01:18
Just some experiments with ruby modules
#
# Example 1
#
# There are two modules, one for instance methods and one for class methods
# Use 'include' and 'extend' separately
#
module Foo1Instance
def foo; "instance foo (#{self.class.name})"; end
end