A (more) complete cheatsheet for Arel, including NamedFunction functions, raw SQL and window functions.
posts = Arel::Table.new(:posts)
posts = Post.arel_table # ActiveRecord
package csrf | |
import "errors" | |
// This is just an example on how to reduce redundant code in Go | |
// Original file: https://github.com/gofiber/csrf/blob/dde96da6711a2cc01709a66730c2e16328b22047/main.go | |
// csrfFromExtractor returns a function can be used to bind to a parameter name to extract a csrf token from a Ctx. | |
func csrfFromExtractor(errMessage string, extractor func(c *fiber.Ctx, param string) string) func(string) func(c *fiber.Ctx) (string, error) { | |
return func(param string) func(c *fiber.Ctx) (string, error) { |
Concurrency is a domain I have wanted to explore for a long time because the locks and the race conditions have always intimidated me. I recall somebody suggesting concurrency patterns in golang because they said "you share the data and not the variables".
Amused by that, I searched for "concurrency in golang" and bumped into this awesome slide by Rob Pike: https://talks.golang.org/2012/waza.slide#1 which does a great job of explaining channels, concurrency patterns and a mini-architecture of load-balancer (also explains the above one-liner).
Let's dig in:
Once in a while, you may need to cleanup resources (containers, volumes, images, networks) ...
// see: https://github.com/chadoe/docker-cleanup-volumes
$ docker volume rm $(docker volume ls -qf dangling=true)
$ docker volume ls -qf dangling=true | xargs -r docker volume rm
# Original Rails controller and action | |
class EmployeesController < ApplicationController | |
def create | |
@employee = Employee.new(employee_params) | |
if @employee.save | |
redirect_to @employee, notice: "Employee #{@employee.name} created" | |
else | |
render :new | |
end |
# Thee will be more information here when I share the entire problem space I'm working on, but | |
# in short, this is preview material for my second talk in a series called "What Computer Scientists Know". | |
# The first talk is on recursion, and goes through several examples., leading up to a problem based | |
# on a simple puzzle that initial estimates based on performance of a previous puzzle would take years | |
# to solve on modern computers with the techniques shown in Ruby. That sets the stage for improving the | |
# performance of that problem with threading, concurrency, and related tuning. | |
# | |
# The second talk is on threading and concurrency, touching on algorithmic performance as well. | |
# Using some knowledge of the problem (board symmetry, illegal moves, etc), we reduce the problem space | |
# to about .5% of what we initially thought it was. Still, the initial single threaded solution took more |
# coding: utf-8 | |
# Can ruby have method names have newlines/be crazy? | |
class BadKitty | |
FACE = " | |
|\\_/| | |
/ @ @ \\ | |
( > º < ) | |
`>>x<<´ |
def dress(dog_or_dogs) | |
dressed_dogs = Array(dog_or_dogs).map {|dog| DogSweater.new(dog) } | |
dog_or_dogs.respond_to?(:each) ? dressed_dogs : dressed_dogs.first | |
end | |
one_dog = dress(Dog.new) | |
all_my_dogs = dress([Dog.new, Dog.new, Dog.new]) |
require 'json' | |
class JsonFormatter | |
METHODS = %w[start close stop start_dump dump_pending dump_failures dump_summary] | |
METHODS.each { |m| define_method(m) { |*a| } } | |
def initialize(out) | |
@output = out | |
@event_id = 0 | |
@passed = true |
require "rspec/core/formatters/progress_formatter" | |
class ApplauseFormatter < RSpec::Core::Formatters::ProgressFormatter | |
def initialize(output) | |
super(output) | |
unless File.exists? "/tmp/applause.mp3" | |
p "Downloading applause for awesomeness" | |
system "wget http://www.soundjay.com/human/applause-1.mp3 -O /tmp/applause.mp3" | |
end | |
end |