Skip to content

Instantly share code, notes, and snippets.

View tiegz's full-sized avatar
👋
Working from home

Tieg Zaharia tiegz

👋
Working from home
View GitHub Profile
module TrueFalseComparison
def <=>(other)
return nil if ![TrueClass, FalseClass].include?(other.class)
self.is_a?(FalseClass) ? (other.is_a?(TrueClass) ? -1 : 0) : (other.is_a?(FalseClass) ? 1 : 0)
end
end
TrueClass.send(:include, TrueFalseComparison)
FalseClass.send(:include, TrueFalseComparison)
class Statistic < ActiveRecord::Base
named_scope :by_month_and_year, :select => "DATE_FORMAT(created_at, '%b %y')", :group => "DATE_FORMAT(created_at, '%b %y')", :order => :created_at
end
>> Statistic.by_month_and_year.size
=> 6 # SELECT count(DATE_FORMAT(created_at, '%b %y')) AS count_date_format_created_at_b_y FROM `statistics`
>> Statistic.by_month_and_year.all.size
=> 5 # SELECT DATE_FORMAT(created_at, '%b %y') FROM `statistics` GROUP BY DATE_FORMAT(created_at, '%b %y') ORDER BY created_at
# Put this in your app config to omit the div that wraps error fields in form_for
ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag }
@tiegz
tiegz / gist:111107
Created May 13, 2009 16:25
An alternative to String#next/#succ for incrementing strings
describe String do
describe "#increment and #increment!" do
it 'should increment nothing to 1' do
"Something".increment.should == "Something1"
end
it 'should increment the digits in the string' do
"789".increment.should == "790"
end
def loggable(logger)
@@logger = logger
def method_missing(meth, *args)
if meth.to_s =~ /^log_/
@@logger.info "#{Time.now.to_s} - #{meth.to_s.split('_')[1..-1].join(' ')}"
else
super
end
end
end
# Rails 2.3.2
# Ruby 1.8.6
# Appears to be weirdness when you override a class method that
# is also a method_missing finder. Check out the SQL output:
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts, :force => true do |t|
t.string :title
end
response body => \037\213\b\000\000\000\000\000\000\0033624550\006\000,/\327T\a\000\000\000
net/http interpretation => 3624550,/?T
response body => \037\213\b\000\000\000\000\000\000\0033624550\001\000\217\272\263\312\a\000\000\000
net/http interpretation => 3624550????
response body => \037\213\b\000\000\000\000\000\000\0033624550\005\000\031\212\264\275\a\000\000\000
net/http interpretation => 3624550???
response body => \037\213\b\000\000\000\000\000\000\0033624550\a\0005\353\272S\a\000\000\000
var λ = function(k){var a=[];while(a.length+1<arguments.length){a.push(arguments[a.length+1])};return function(o){return((o[k]&&o[k]instanceof Function)?o[k].apply(this,a):o[k]);}};
var ary = [
{num:1, f:function(a,b,c){console.log("A: " + a + "," + b + "," + c);}},
{num:2, f:function(a,b,c){console.log("B: " + a + "," + b + "," + c);}},
{num:3, f:function(a,b,c){console.log("C: " + a + "," + b + "," + c);}}
];
ary.forEach(λ('f',1,2,3));
# => A: 1,2,3
// http://localhost:60210/api/?qid=anvil_songofpain_backtobasics&poll=2&call_id=1273084702020&method=get_results&auth=0D85A28B-BBDA-4F26-96E6-4FAEA0421A0E&jsonp=Playdar.client.handle_results
Playdar.client.handle_results({"qid":"anvil_songofpain_backtobasics","poll_interval":1000,"poll_limit":6,"query":{"artist":"Anvil","album":"Back to Basics","track":"Song Of Pain"},"solved":false,"results":[{"sid":"2C2556F9-FFB2-4BBA-93F1-9A8EEA93B058","artist":"Anvil","track":"Keep It Up","album":"Back To Basics","mimetype":"audio/mpeg","score":0.6666666666666666,"duration":230,"bitrate":234,"size":2097152,"source":"tieg-zaharia"}]});
// http://localhost:60210/api/?qid=anvil_keepitup_backtobasics&poll=1&call_id=1273084700988&method=get_results&auth=0D85A28B-BBDA-4F26-96E6-4FAEA0421A0E&jsonp=Playdar.client.handle_results
Playdar.client.handle_results({"qid":"anvil_keepitup_backtobasics","poll_interval":1000,"poll_limit":6,"query":{"artist":"Anvil","album":"Back to Basics","track":"Keep it Up"},"solved":true,"results":[{
{ scopeName = 'source.js';
comment = 'JavaScript Syntax: version 2.0';
fileTypes = ( 'js', 'htc', 'jsx' );
foldingStartMarker = '^.*\bfunction\s*(\w+\s*)?\([^\)]*\)(\s*\{[^\}]*)?\s*$';
foldingStopMarker = '^\s*\}';
patterns = (
{ name = 'meta.class.js';
comment = 'match stuff like: Sound.prototype = { … } when extending an object';
match = '([a-zA-Z_?.$][\w?.$]*)\.(prototype)\s*(=)\s*';
captures = {