Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View deivinsontejeda's full-sized avatar
🎯

Deivinson Tejeda deivinsontejeda

🎯
  • Santiago of Chile
View GitHub Profile
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) {
@ProGM
ProGM / arel_cheatsheet_on_steroids.md
Last active April 19, 2024 04:06
Arel cheatsheet on Steroids

Arel Cheatsheet on Steroids

A (more) complete cheatsheet for Arel, including NamedFunction functions, raw SQL and window functions.

Tables

posts = Arel::Table.new(:posts)
posts = Post.arel_table # ActiveRecord

Table alias

@rushilgupta
rushilgupta / GoConcurrency.md
Last active January 25, 2024 14:59
Concurrency in golang and a mini Load-balancer

INTRO

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:

Goroutines

@bastman
bastman / docker-cleanup-resources.md
Created March 31, 2016 05:55
docker cleanup guide: containers, images, volumes, networks

Docker - How to cleanup (unused) resources

Once in a while, you may need to cleanup resources (containers, volumes, images, networks) ...

delete volumes

// 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

@dhh
dhh / test_induced_design_damage.rb
Last active June 22, 2023 06:18
This is an extraction from Jim Weirich's "Decoupling from Rails" talk, which explained how to apply the hexagonal design pattern to make every layer of your application easily unit testable (without touching the database etc). It only seeks to extract a single method, the EmployeesController#create method, to illustrate the design damage that's …
# 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
@bokmann
bokmann / JRuby Awesome Performance
Last active August 31, 2023 07:32
brief summary of massive performance improvements with JRuby
# 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<<´
@bkeepers
bkeepers / gist:6002211
Last active December 19, 2015 18:58
I'm looking for a better pattern for defining a method that takes a single object or an array of objects as an argument, does something with them, and then returns either a single object or an Array depending on what was passed to it.
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
@dqminh
dqminh / applause_formatter.rb
Last active December 18, 2015 04:59
rspec formatter that applause you when your tests pass
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