Skip to content

Instantly share code, notes, and snippets.

View thdaraujo's full-sized avatar
🧡
coding

Thiago Araujo thdaraujo

🧡
coding
View GitHub Profile
@thdaraujo
thdaraujo / deprecator_mixin.rb
Created February 22, 2024 03:18
An example of how to deprecate constants in Ruby.
# based on ActiveSupport
# https://github.com/rails/rails/blob/6f0d1ad14b92b9f5906e44740fce8b4f1c7075dc/activesupport/lib/active_support/deprecation/constant_accessor.rb#L32
module MyLib
module Deprecator
def self.included(base)
extension = Module.new do
def const_missing(missing_const_name)
puts "deprecator - const missing #{missing_const_name}"
if class_variable_defined?(:@@_deprecated_constants)
# Example of how to create a lazy generator for Faker that
# can exclude certain values.
#
# How to run:
# ruby faker_excluding_generator.rb
#
require "faker"
require "test/unit"
@thdaraujo
thdaraujo / enum_in_order_of_test.rb
Last active January 31, 2024 16:10
Rails `in_order_of` with pg enum columns
# how to run:
# $ ruby enum_in_order_of_test.rb
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
@thdaraujo
thdaraujo / pg_default_error.rb
Last active December 7, 2023 18:09
timestamp loading issue on Rails 6.1
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails', '= 6.1'
@thdaraujo
thdaraujo / group_query_example.rb
Last active November 16, 2022 05:08
Group orders by customers and products
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails'
@thdaraujo
thdaraujo / complex_arel_ordering_example.rb
Last active June 2, 2021 05:26
Example of a complex order-by Arel query
# how to run: `$ ruby complex_arel_ordering_example.rb`
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails'
@thdaraujo
thdaraujo / cover.md
Last active March 11, 2021 19:01
Carbon-neutral Rails

Carbon-Neutral Rails

# Gemfile
gem 'patch_ruby'
require 'patch_ruby'
@thdaraujo
thdaraujo / foxyproxy.json
Created April 3, 2019 19:11 — forked from apetresc/foxyproxy.json
A FoxyProxy 6.x (for Firefox) version of the AWS EMR filters
{
"mode": "patterns",
"proxySettings": [
{
"title": "EMR SOCKS Proxy",
"type": 3,
"color": "#cc8c2a",
"address": "localhost",
"port": 8157,
"proxyDNS": true,
(defn average [x y] (* 0.5 (+ x y)))
(defn improve [guess x] (average guess (/ x guess)))
(defn square [x] (* x x))
(defn abs [x] (if (< 0 x) x (- 0 x)))
(defn good-enough? [guess x]
(< (abs (- x (square guess))) 0.0000001))
@thdaraujo
thdaraujo / min_abs_sum.rb
Created April 7, 2018 23:13
Given array of integers, find the lowest absolute sum of elements.
def min_abs_sum(a)
n = a.size
[1, -1].repeated_permutation(n).map do |s|
val(a,s).abs
end.min
end
def val(a,s)
a.map.with_index{|e, i| a[i] * s[i]}.inject(:+)
end