Let's code

Starting a New Ambitious Project

For a very short time, I used the Spacemacs emacs distribution. It’s very nice, but I ended up backing to vim and eventually, I moved to VS Code. Code editors away, when I was using Spacemacs, I found the Magit git client. Magit is an interface to the version control system Git, implemented as an Emacs package. Magit aspires to be a complete Git porcelain. While we cannot (yet) claim that Magit wraps and improves upon each and every Git command, it is complete enough to allow even experienced Git users to perform almost all of their daily version control tasks directly from within Emacs.

json: unmarshal custom date formats

When we are building a JSON API, many times we have to deal with dates that we can’t have control of the layout. Let’s see a common example using a format that Go knows by default. type Checkin struct { Timestamp time.Time `json:"timestamp"` User string `json:"user"` } func main() { j := `{"timestamp":"2016-11-02T08:18:20Z", "user":"John Doe"}` var c Checkin // error handling omitted for simplicity (don't do this). json.Unmarshal([]byte(j), &c) fmt.Println(c) } Run on Playground.

Variadic functions in Go

Let’s create a function that we can pass 3 numbers as arguments and them it returns the average of this numbers. func average(num1, num2, num3 float64) float64 { avg := (num1 + num2 + num3) / 3 return avg } and then, you call it like this func main() { fmt.Println(average(2, 4, 6)) } Le’ts run to see if works $ go run average.go 4 It works. Super easy, but not so useful.

Novos finders no rails 4

Com a entrada do Rails 4 alguns métodos mudaram. O métodos “mágicos” que a gente conhecia como find_all_by_name foram marcados como deprecated e serão removidos no Rails 4.1. Vamos ver alguns exemplos: # Ao invés de usar os finders dinâmicos, agora usamos o where. Rails 3: Person.find_all_by_first_name('John') Rails 4: Person.where(first_name: 'John') # apartir dai utilizamos podemos utilizar algumas métodos para filtrar mais # First e Last Person.where(first_name: 'John').first Person.where(first_name: 'John').last Find, create, initialize Person.

Quick tip - Retornando mais de um valor em um método

O comportamento padrão de um método Ruby, é retornar o último valor que ele possui. Então se tivermos o seguinte código: def calc(x, y) a = x + y b = x - y c = x * y d = x / y end Não importa quantas variáveis você tenha definido, o retorno sempre será a última, no caso d calc(10, 5) => 2 Mas, em algum momento podemos precisar que esse método retorne mais de um valor.