Skip to content

Instantly share code, notes, and snippets.

View horacio's full-sized avatar
🧩

Horacio Bertorello horacio

🧩
  • Buenos Aires, Argentina
  • 22:32 (UTC -03:00)
View GitHub Profile
@rtomayko
rtomayko / Tests Is Wrong
Created January 28, 2009 20:50
Why "require 'rubygems'" In Your Library/App/Tests Is Wrong
In response to all the responses to:
http://twitter.com/rtomayko/status/1155906157
You should never do this in a source file included with your library,
app, or tests:
require 'rubygems'
The system I use to manage my $LOAD_PATH is not your library/app/tests
;; this file is a walkthrough of Moustache features, a web framework for Clojure
;; http://github.com/cgrand/moustache/tree/master
;; Moustache allows to declare routes, apply middlewares and dispatch on http methods.
;; Moustache is compatible with all frameworks built on Ring, including Compojure
(ns demo
(:use net.cgrand.moustache)
(:use [ring.adapter.jetty :only [run-jetty]])) ;; hmmm Ring without servlets
@mperham
mperham / ex1.rb
Created April 19, 2011 02:43
Simple ping/pong with Actors
require 'actor'
# Execute with Rubinius: rbx ex1.rb
def error_loop(&block)
loop(&block)
rescue Exception => ex
puts ex.message
puts ex.backtrace.join("\n")
end
@sandosh
sandosh / gist:959969
Created May 6, 2011 23:06
Underscore.js - compact an object just like compact function for array
_.mixin({
capitalize : function(string) {
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
},
compactObject: function(to_clean) {
_.map(to_clean, function(value, key, to_clean) {
if (_.isNull(value) || _.isUndefined(value) || (_.isString(value) && _.trim(value).length === 0) || (_.isBoolean(value) && value === false)) {
delete to_clean[key];
}
});
@zumbojo
zumbojo / bijective.rb
Created July 9, 2011 22:09
Simple bijective function (base(n) encode/decode)
# Simple bijective function
# Basically encodes any integer into a base(n) string,
# where n is ALPHABET.length.
# Based on pseudocode from http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener/742047#742047
ALPHABET =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(//)
# make your own alphabet using:
# (('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a).shuffle.join
@guilleiguaran
guilleiguaran / active_model_compliant.rb
Created July 21, 2011 06:06
Ohm::ActiveModelCompliant, allows to Ohm (Redis OHM) to be partially compliant with ActiveModel (except errors)
require 'active_model'
module Ohm
module ActiveModelCompliant
def self.included(base)
base.send(:extend, ActiveModel::Naming)
base.send(:include, ActiveModel::Conversion)
base.send(:include, InstanceMethods)
end
@wtaysom
wtaysom / where_is.rb
Created September 23, 2011 08:57
A little Ruby module for finding the source location where class and methods are defined.
module Where
class <<self
attr_accessor :editor
def is_proc(proc)
source_location(proc)
end
def is_method(klass, method_name)
source_location(klass.method(method_name))
@pauljamesrussell
pauljamesrussell / spec_helper.rb
Created November 9, 2011 23:15
RSpec matcher for ensuring a model is accepting nested attributes for an association, and accepting/rejecting the right values.
# Use: it { should accept_nested_attributes_for(:association_name).and_accept({valid_values => true}).but_reject({ :reject_if_nil => nil })}
RSpec::Matchers.define :accept_nested_attributes_for do |association|
match do |model|
@model = model
@nested_att_present = model.respond_to?("#{association}_attributes=".to_sym)
if @nested_att_present && @reject
model.send("#{association}_attributes=".to_sym,[@reject])
@reject_success = model.send("#{association}").empty?
end
model.send("#{association}").clear
module DelayedJob
module Matchers
def enqueue_delayed_job(handler)
DelayedJobMatcher.new handler
end
class DelayedJobMatcher
def initialize(handler)
@handler = handler
@attributes = {}
@stevenharman
stevenharman / select_from_subquery.sql
Created January 21, 2013 20:44
Using Arel/ActiveRecord to execute a subquery, including a SUM.
select fund_sums.total, fund_products.name
from (
select sum(allocation_amount) total, fund_product_id
from transactions
where transactions.investor_id = 490
group by fund_product_id
) fund_sums
left join fund_products on fund_sums.fund_product_id = fund_products.id