Skip to content

Instantly share code, notes, and snippets.

View reu's full-sized avatar

Rodrigo Navarro reu

View GitHub Profile
@reu
reu / benchmark.rb
Created April 16, 2012 17:00
NUM2DBL slow BigDecimal convertion
# Passos para rodar:
# 1. ruby extconf.rb
# 2. make
# 3. ruby benchmark.rb
require 'benchmark'
require 'bigdecimal'
require './test'
n = 50
@reu
reu / README.md
Created May 21, 2012 14:36
Ruby annotations

Imagine this case:

class Account < ActiveRecord::Base
  def transfer(other, quantity)
    tries = 0
    begin
      tries += 1
      transaction do
        self.quantity -= quantity
@reu
reu / mixin.coffee
Created June 7, 2012 23:55
CoffeeScript mixins
# Of course we can make something like this
class Module
@include: (mixin) ->
this.prototype[name] = method for name, method of mixin
Saveable =
save: -> alert "#{@name} saved!"
class User extends Module
require "thread"
class Account
attr_reader :amount
def initialize(amount)
@amount = amount
end
def debit(amount)
@reu
reu / presenters.rb
Created June 16, 2012 20:03
Simplest "presenter" ever
class Presenter < BasicObject
def initialize(object)
@object = object
end
def method_missing(method, *args, &block)
@object.send method, *args, &block
end
module RailsHelpers
@reu
reu / password_prompt.rb
Created October 19, 2012 03:19
Password prompt for CLIs
def password_prompt(message = "Inform your password: ")
puts message
begin
system "stty -echo -icanon"
gets.rstrip
ensure
system "stty echo icanon"
end
end
@reu
reu / async.js
Created November 21, 2012 12:13
Async javascript loading
var async = function(url, callback) {
var script = document.createElement("script");
script.src = url;
if (callback != null) {
callback.done = false;
script.onreadystatechange = s.onload = function() {
var state = script.readyState;
@reu
reu / pub-sub.js
Created April 9, 2013 01:51
node.js redis pub-sub example
var redis = require("redis")
, subscriber = redis.createClient()
, publisher = redis.createClient();
subscriber.on("message", function(channel, message) {
console.log("Message '" + message + "' on channel '" + channel + "' arrived!")
});
subscriber.subscribe("test");
@reu
reu / raw_sockets.rb
Created June 5, 2013 17:28
Simple sniffer in Ruby using pcap
require 'rubygems'
require 'pcaprub'
capture = PCAPRUB::Pcap.open_live('lo0', 65535, true, 0)
capture.setfilter('icmp')
while 1==1
puts(capture.stats())
pkt = capture.next()
if pkt
puts "captured packet"
function inherit(child, parent) {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = child;
}
var Person = function Person(firstName, lastName) {
if (!firstName || !lastName) throw "Primeiro e último nome requerido!";
this.firstName = firstName;
this.lastName = lastName;