Skip to content

Instantly share code, notes, and snippets.

@benoittgt
benoittgt / benchmark_define.rb
Last active November 3, 2023 10:45
Struct.new VS Data.define
require 'benchmark/ips'
DataS = Struct.new(:encoder, :values) do # :nodoc:
def hash
[encoder, values].hash
end
end
DataD = Data.define(:encoder, :values) do # :nodoc:
def hash
require 'gvl-tracing'
require 'net/http'
class ThreadPool
def initialize(size)
@size = size
@jobs = Queue.new # FIFO https://rubyapi.org/3.2/o/thread/queue
@pool = Array.new(@size) do |i|
Thread.new do
catch(:exit) do
# ENGLISH ABOVE, FRENCH BELOW -- VERSION FRANCAISE CI DESSOUS
# You want to tally something in Rails and you like Ruby so you've written
Object.pluck(:field).tally
# and the results look OK so you don't look further
{'ruby' => 999, 'python' => 300, 'java' => 42, 'C' => 1}
# But you're actually asking the database to do only SELECT objects.field, fetch 1342 records, and counting in Ruby
# Databases are quite cool and fast, and you'd rather do
"SELECT objects.field, COUNT(objects.field) AS objects_count FROM objects GROUP BY objects.field"
@nnps255
nnps255 / create-cloud-template.sh
Last active January 23, 2021 15:52 — forked from chriswayg/create-cloud-template.sh
This script will download a cloud image of many Linux distros and create a Proxmox 6 KVM template from it.
#!/bin/bash
set -o errexit
clear
printf "\n*** This script will download a cloud image and create a Proxmox VM template from it. ***\n\n"
### HOW TO USE
### Pre-req:
### - run on a Proxmox 6 server
### - a dhcp server should be active on vmbr1
@dhh
dhh / Gemfile
Created June 24, 2020 22:23
HEY's Gemfile
ruby '2.7.1'
gem 'rails', github: 'rails/rails'
gem 'tzinfo-data', '>= 1.2016.7' # Don't rely on OSX/Linux timezone data
# Action Text
gem 'actiontext', github: 'basecamp/actiontext', ref: 'okra'
gem 'okra', github: 'basecamp/okra'
# Drivers
@yann-yinn
yann-yinn / what-is-a-bounty.md
Last active July 25, 2019 12:06
What is a bounty ?

[EXPERIMENTATION ]Qu'est ce qu'un bounty ?

Description

Un bounty est une demande de micro-prestation rémunérée (une heure à une journée max), en générale plutôt urgente, entre deux développeurs freelances qui a pour but de mieux faire circuler les expertises: c'est un développeur qui galère sur une problématique technologique précise sur laquelle il est peu expérimenté; et qui cherche de l'aide de la part d'un autre développeur plus expérimenté sur cette problématique, capable de la résoudre bien plus rapidement que lui.

Exemple : Paul à un souci de configuration Docker et y passe la journée en faisant de la merde alors que Bill aurait mis une heure à fixer sa configuration. Si Bill, qui était entre deux contrats, avait aidé Paul, il aurait pu gagner 60 euros en une heure et Paul aurait gagné 7 heures de productivité : tout le monde aurait gagné du temps et de l'argent.

Exemples de bounty:

@nateberkopec
nateberkopec / correction.md
Last active February 14, 2024 16:22
A Simple Correction

In yesterday's post I said, in relation to "how does .present? work on ActiveRecord::Relation", I said that present? performs an existence check SELECT 1 AS one FROM ... LIMIT 1 because it calls exists? underneath. This is actually wrong - it loads the relation.

Jonathan Mast corrected me on Twitter. It turns out, I should have paid closer attention! Here is the actual implementation of blank? on ActiveRecord::Relation on Rails master:

# Returns true if relation is blank.
def blank?
  records.blank?
end
@benoittgt
benoittgt / measure_ruby_process.rb
Last active October 27, 2018 10:06
Graph process consumption with psrecord
child_pid = spawn "psrecord #{Process.pid} --plot plot_#{Time.now.strftime('%H_%M_%S')}.png"
a = []
10_000_000.times { |i| a << i.to_s }
puts Process.kill('SIGINT', child_pid)
puts "continue scripting"
@mhofman
mhofman / HAProxy-transparent-web-services-routing.md
Last active April 4, 2024 01:20
Leverage HAProxy to transparently route requests to web services identified by host name.

Web Service Fronting

Multiple Web properties on a single IP address

Hosting multiple websites on a single public IP address on the standard HTTP(S) ports is relatively easy with popular web servers like Apache, Nginx and lighttpd all supporting Virtual Hosts.
For Web Services which bundle their own HTTP server, things get more complicated, unless their HTTP stack can be shared somehow. More often than not, the application's HTTP stack listens directly on a dedicated TCP port.

Hosting multiple services on a single IP then requires using a fronting server listening on the standard HTTP port, and routing to the right backend service based on the host name or the path sent by the client.
Path based routing is cumbersome, usually requiring either the service to be aware of the path prefix, or a rewrite by the HTTP fronting server of all absolute URLs in the requests and responses.
Hostname based routing is more straightforward. The fronting server can just look at the [HTTP/1.1 Host header](https://tools

@carlosramireziii
carlosramireziii / attached_validator.rb
Last active April 10, 2024 11:11
A validator and RSpec matcher for requiring an attachment using Active Storage
class AttachedValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors.add(attribute, :attached, options) unless value.attached?
end
end