Skip to content

Instantly share code, notes, and snippets.

View tdantas's full-sized avatar
🏠
Working from home

Thiago Dantas tdantas

🏠
Working from home
View GitHub Profile
@tdantas
tdantas / mongo.rb
Created November 8, 2011 22:53 — forked from rodriguezartav/mongo.rb
config/initializers/mongo.rb it setups mongomapper to be used locally and in heroku with MongoHQ
MongoMapper.config = {
Rails.env => { 'uri' => ENV['MONGOHQ_URL'] ||
"mongodb://localhost/rubyconf_simple_app-#{Rails.env}-1" } }
MongoMapper.connect(Rails.env)
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
MongoMapper.connection.connect if forked
end
@tdantas
tdantas / gist:1588474
Created January 10, 2012 10:58 — forked from dshaw/gist:378192
(function(window,document,undefined){ ... })(this,this.document);
// everyone's new favorite closure pattern:
(function(window,document,undefined){ ... })(this,this.document);
// when minified:
(function(w,d,u){ ... })(this,this.document);
// which means all uses of window/document/undefined inside the closure
// will be single-lettered, so big gains in minification.
// it also will speed up scope chain traversal a tiny tiny little bit.
@tdantas
tdantas / app.rb
Created June 28, 2012 22:09 — forked from cpatni/app.rb
unique calculation using redis
require 'sinatra'
require 'redis'
require 'json'
require 'date'
class String
def &(str)
result = ''
result.force_encoding("BINARY")
# -*- encoding : utf-8 -*-
require "spec_helper"
describe Notification do
describe "new_question" do
let(:user) { Factory.create(:user, name: 'Dart Vader', email: 'dart@example.com') }
let(:category) { Factory.create(:category, name: 'Saúde') }
let(:owner) { Factory.create(:user, name: 'Jaspion') }
let(:question) { Factory.create(:question, user: owner, category: category) }
let(:mailer) { Notification.new_question(question.id, user.id) }
@tdantas
tdantas / question.js
Created October 28, 2012 20:05
programming async trick.
//Question:
// Environment: NodeJS
// Lets suppose that you have a collection of items, and your function must persist all items using the
//database.insert(item, callback ) function. When you finish the task, you should call the callback function.
// If one item raise a error, the callback(err) must be called and never call the callback anymore.
function saveBulk(items, callback) {
// Your code here
@tdantas
tdantas / trick
Created November 13, 2012 17:22
Programming Trick
Objective:
Write a program that prints out a multiplication table of the first 10 prime numbers.
The program must run from the command line and print to screen 1 table.
Across the top and down the left side should be the 10 primes, and the body of the table should
contain the product of multiplying these numbers.
Please include Rspec tests.
P.s. the prime numbers should be calculated, rather than using Ruby’s Prime Method
## OUTPUT ##
class Fixnum
def prime?
(2..Math.sqrt(self).to_i).each do |number|
return false if (self % number == 0)
end
true
end
end
class PrimeMatrix
@tdantas
tdantas / Flavour.rb
Created November 16, 2012 12:41
Flavour - The module to avoid ifs when your model is State driven behaviour
module Flavour
module ClassMethods
def watch(&block)
@watchable = block
end
def watchable
@watchable

Simplistic Full-Text Search With Redis's Sorted Sets

Howto

git clone git://gist.github.com/923934.git redisearch

cd redisearch
# Build an inverted index for a full-text search engine with Redis.
# Copyright (C) 2009 Salvatore Sanfilippo. Under the BSD License.
# USAGE:
#
# ruby invertedindex.rb add somedir/*.c
# ruby invertedindex.rb add somedir/*.txt
# ruby search your query string
require 'rubygems'
require 'redis'