Skip to content

Instantly share code, notes, and snippets.

View gbarrancos's full-sized avatar

Gustavo Barrancos gbarrancos

  • Nubank
  • São Paulo - Brazil
View GitHub Profile
# in the model
validates_with CoordinatesValidator, :fields => [:location]
# Error messaging needs improvement. Since this validator will only whine if someone is forging requests
# with invalid data, i'm leaving it as it is.
class CoordinatesValidator < ActiveModel::Validator
def validate(record)
if options[:fields].any? { |f| invalid_latlng?(record.send(f)) }
@gbarrancos
gbarrancos / field_converters.rb
Created January 3, 2011 20:33
Mongoid doesn't provide typed Array declarations, so you have to to convert them by hand when pushing params from request to your model instance. This one helps by defining the conversion callback automatically
# class YoClass
# include Mongoid::Document
# include FieldConverters
#
# field :an_array, type => Array
# convert_array [:an_array], :to_array_of => Integer, :before => :validate
# end
module FieldConverters
def self.included(base)
@gbarrancos
gbarrancos / loop.clj
Created April 24, 2011 15:17
Loop + Maping with a side effecty fn over a seq
(defn side-effecty-fn [arg]
(print (str "Hit the fn: " arg "\n" ))
)
(defn loopy [begin end]
(loop [current begin]
(if (> current end)
"Done"
(do
(print (str "Calling with " current "\n"))
@gbarrancos
gbarrancos / cutest_example.c
Created April 28, 2011 21:48
An example of a CuTest-based unit test
#include <stdlib.h>
#include <stdio.h>
#include "CuTest.h"
#include "../ep2_gustavob.h"
/********* Infix to Postfix Conversion *********/
void test_single_expr (CuTest* testContext) {
char actual[BUF_SIZE];
@gbarrancos
gbarrancos / gist:3277138
Created August 6, 2012 17:57
7 Tips for Successful Self Learning

7 Tips for Successful Self Learning - by Bradford Cross

No matter what, you're going to have to learn most everything on your own anyway. Self-learning is hard. Regardless of where, when or how you learn - being a good self-learner will maximize your potential.

In this post, Hamilton Ulmer (an almost-done Stanford stats masters student) and I, will explore seven ways to become a great self-learner.

The longest path is the shortest and the shortest path is the longest

@gbarrancos
gbarrancos / 6502_ex_1
Created February 13, 2013 20:47
6502 assembly first time. Need a way to loop the entire video memory region [$200,$05FF] only using 8-bit registers
LDY #$00 ;[$0200, $05ff] - video memory region
LDX #$00 ;[#$00,#$0F] - possible color values
shift_color: ;looping through the 16 possible colors
INX
CPX #$0F
BNE paint
LDX #$00
paint:
@gbarrancos
gbarrancos / fib_closure.go
Last active August 29, 2015 13:58
Fibonacci implementation using closures in Go
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
seqIndex := -1
lastFib := 0
beforeLastFib := 0
package main
import (
"io"
"os"
"strings"
)
var rot13Map = []string {
"a", "n",
@gbarrancos
gbarrancos / golang_tour_72.go
Last active August 29, 2015 14:00
Equivalent Binary Trees Exercise from Golang Tour
package main
import (
"code.google.com/p/go-tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.

Mastering Programming - by Kent Beck

From years of watching master programmers, I have observed certain common patterns in their workflows. From years of coaching skilled journeyman programmers, I have observed the absence of those patterns. I have seen what a difference introducing the patterns can make. Here are ways effective programmers get the most out of their precious 3e9 seconds on the planet. The theme here is scaling your brain. The journeyman learns to solve bigger problems by solving more problems at once. The master learns to solve even bigger problems than that by solving fewer problems at once. Part of the wisdom is subdividing so that integrating the separate solutions will be a smaller problem than just solving them together.

Time

Slicing - Take a big project, cut it into thin slices, and rearrange the slices to suit your context. I can always slice projects finer and I can always find new permutations of the slices that meet different needs