Skip to content

Instantly share code, notes, and snippets.

View simonbaird's full-sized avatar
💭
🤔

Simon Baird simonbaird

💭
🤔
View GitHub Profile
@simonbaird
simonbaird / with_stubbed_const.rb
Created March 13, 2015 01:26
Stubbing a constant in ruby
module WithStubbedConst
def with_stubbed_const(consts, scope=self.class)
stash = {}
consts.each_pair do |key, val|
stash[key] = scope.send(:remove_const, key)
scope.send(:const_set, key, val)
end
begin
yield
@simonbaird
simonbaird / phpa.php
Created February 12, 2010 02:25
Nicer handling of exceptions. See http://david.acz.org/phpa/
#!/usr/local/bin/php -qC
<?
/*
$Id: phpa.php 2008/04/28 $
David Phillips <david@acz.org>
*/
__phpa__setup();
__phpa__print_info();
#
# Use this in models for status codes etc. Will dynamically create a class constant
# for each row in the table. The name of the constant will be based on the field specified.
# Typically it would be code, though name might be useful in some cases.
#
# This will build the constants once on rails startup. It only hits the database once, the find(:all),
# so should be reasonably efficient as long as it's used for small tables only.
#
# Put this in lib/create_id_constants.rb
#
[Development]>> puts Time.now; (-5..5).each { |w| t = Time.now + w.weeks; puts "now and #{'%2d'%w} weeks: #{t.beginning_of_fortnight} - #{t.end_of_fortnight}" }; nil
Wed Oct 06 16:29:52 +1000 2010
now and -5 weeks: Mon Aug 30 00:00:00 +1000 2010 - Sun Sep 12 23:59:59 +1000 2010
now and -4 weeks: Mon Aug 30 00:00:00 +1000 2010 - Sun Sep 12 23:59:59 +1000 2010
now and -3 weeks: Mon Sep 13 00:00:00 +1000 2010 - Sun Sep 26 23:59:59 +1000 2010
now and -2 weeks: Mon Sep 13 00:00:00 +1000 2010 - Sun Sep 26 23:59:59 +1000 2010
now and -1 weeks: Mon Sep 27 00:00:00 +1000 2010 - Sun Oct 10 23:59:59 +1000 2010
now and 0 weeks: Mon Sep 27 00:00:00 +1000 2010 - Sun Oct 10 23:59:59 +1000 2010
now and 1 weeks: Mon Oct 11 00:00:00 +1000 2010 - Sun Oct 24 23:59:59 +1000 2010
now and 2 weeks: Mon Oct 11 00:00:00 +1000 2010 - Sun Oct 24 23:59:59 +1000 2010
@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
@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
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 / 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)
# See http://tauday.com/
module Math
TAU = PI * 2.0
end
if defined? BigMath
module BigMath
module_function
def TAU(prec)