Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View tjsingleton's full-sized avatar

TJ Singleton tjsingleton

View GitHub Profile
@tjsingleton
tjsingleton / accordian-menu.php
Created March 26, 2010 17:10
Refactoring a piece of code I came across that generates a menu. Saved each step so you can follow along. Opps, forgot to save the original code :)
<div id="accordion">
<?php
// just cleaning up formatting
define("MASTER_CAT_ID", 280);
define("CHILD_LIMIT", 200);
define("COMMON_GET_PAGE_ARGS", 'sort_column=menu_order&sort_order=asc&number='.CHILD_LIMIT);
function get_children_pages_of($cat_id) {
return get_pages(COMMON_GET_PAGE_ARGS.'&child_of='.$cat_id.'&parent='.$cat_id);
}
<?php
$Members = new Pod('members');
$Members->findRecords($orderby = 'city ASC', 100);
// group members by city
$members_by_city = array();
while ($data = $Members->fetchRecord()) {
$city_name = $data["city"];
if(!$members_by_city[$city_name]) {
$members_by_city[$city_name] = array($data);
# requires Ruby 1.9
require "open-uri"
VALID_FORMAT = "[VALID] %s"
INVALID_FORMAT = "[INVALID] %s"
def valid?(url)
open(url).read.force_encoding("UTF-8").valid_encoding?
end
~/Projects/Personal/demo $ rake db:migrate --trace
(in /Users/tjsingleton/Projects/Personal/demo)
rake aborted!
undefined method `insert' for Paperclip::Railtie:Class
/Users/tjsingleton/.rvm/gems/ruby-1.9.1-p378/gems/paperclip-2.3.2/lib/paperclip/railtie.rb:8:in `block in <class:Railtie>'
/Users/tjsingleton/.rvm/gems/ruby-1.9.1-p378/gems/activesupport-3.0.0.beta3/lib/active_support/lazy_load_hooks.rb:7:in `instance_eval'
/Users/tjsingleton/.rvm/gems/ruby-1.9.1-p378/gems/activesupport-3.0.0.beta3/lib/active_support/lazy_load_hooks.rb:7:in `on_load'
/Users/tjsingleton/.rvm/gems/ruby-1.9.1-p378/gems/paperclip-2.3.2/lib/paperclip/railtie.rb:7:in `<class:Railtie>'
/Users/tjsingleton/.rvm/gems/ruby-1.9.1-p378/gems/paperclip-2.3.2/lib/paperclip/railtie.rb:6:in `<module:Paperclip>'
/Users/tjsingleton/.rvm/gems/ruby-1.9.1-p378/gems/paperclip-2.3.2/lib/paperclip/railtie.rb:3:in `<top (required)>'
# you can turn two tests into one if you are testing almost the same thing
# example
before(:each) do
@project = Project.new
end
it "should contain errors on a blank url" do
@project.save
@project.errors.should include({:url=>["can't be blank"]})
end
# you can turn two tests into one if you are testing almost the same thing
# example
before(:each) do
@project = Project.new
end
it "should contain errors on a blank url" do
@project.save
@project.errors.should include({:url=>["can't be blank"]})
end
require "benchmark"
N = 1_000_000
Benchmark.bmbm do |sample|
HAYSTACK = "qqq100601.txt"
PATTERN = /\A([a-z]+)/
raise unless HAYSTACK[PATTERN, 1] == HAYSTACK.match(PATTERN)[0]
@tjsingleton
tjsingleton / benchmarked_block.rb
Created July 7, 2010 21:20
RMU Entrance Exam Multi-VM Benchmarks
n.times { doc.add_text(msg) }
By elasped time
+-------------------------+-----------+-----+------+-------+--------+-----------+-----------+
| Name | Start Mem | 100 | 1000 | 10000 | 100000 | Final Mem | Time |
+-------------------------+-----------+-----+------+-------+--------+-----------+-----------+
| ali_rizvi | 4896 | 100 | 804 | 10452 | 121132 | 126028 | 1.006372 |
| ali_rizvi_fixed | 4896 | 100 | 944 | 11100 | 121868 | 126764 | 1.214406 |
| ali_rizvi_fixed_inc | 4896 | 100 | 948 | 11100 | 121868 | 126764 | 1.265896 |
| alberto_fernandez_capel | 4896 | 104 | 396 | 4124 | 37372 | 42268 | 4.254412 |
| tim_liner | 4896 | 104 | 288 | 4836 | 35284 | 40180 | 18.820256 |
| walton_hoops | 4896 | 108 | 536 | 5524 | 36592 | 41488 | 23.587876 |
By elasped time
+---------------------------+-----------+-----+------+-------+--------+-----------+-----------+
| Name | Start Mem | 100 | 1000 | 10000 | 100000 | Final Mem | Time |
+---------------------------+-----------+-----+------+-------+--------+-----------+-----------+
| ricardo_mendes_refactored | 10292 | 20 | 20 | 696 | 4420 | 14712 | 0.228459 |
| ali_rizvi | 10784 | 56 | 404 | 2568 | 31824 | 42608 | 0.323051 |
| ali_rizvi_fixed_inc | 9380 | 44 | 236 | 2096 | 32744 | 42124 | 0.370307 |
| ali_rizvi_fixed | 10048 | 56 | 428 | 2092 | 32100 | 42148 | 0.371651 |
| alberto_fernandez_capel | 10764 | 36 | 364 | 888 | 6288 | 17052 | 0.618822 |
| tj_singleton_refactored | 8232 | 20 | 300 | 828 | 16676 | 24908 | 0.733384 |