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
# | |
# 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 |
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
# | |
# 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! |
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
# See http://tauday.com/ | |
module Math | |
TAU = PI * 2.0 | |
end | |
if defined? BigMath | |
module BigMath | |
module_function | |
def TAU(prec) |
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
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) |
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
# 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' | |
} |
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
# 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) |
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 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 |
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 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 |
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
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 |
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
# | |
# 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 |