Skip to content

Instantly share code, notes, and snippets.

Avatar
🇧🇷

Glauco Custódio glaucocustodio

🇧🇷
View GitHub Profile
@glaucocustodio
glaucocustodio / auto_formatting_changed_ruby_files_with_standard.md
Last active August 8, 2021 19:29
Auto formatting changed Ruby files with Standard
View auto_formatting_changed_ruby_files_with_standard.md

Prerequisites

Create a git pre commit hook and give it permission to execute:

touch .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
@glaucocustodio
glaucocustodio / fixed_thread_pool_example.rb
Last active March 29, 2022 09:28
FixedThreadPool / Concurrent::Future usage example (concurrent-ruby gem)
View fixed_thread_pool_example.rb
# For more: https://github.com/ruby-concurrency/concurrent-ruby
num_threads = Concurrent.processor_count # or whatever you prefer like 4
thread_pool = Concurrent::FixedThreadPool.new(num_threads)
products = Product.all
executors = products.map { |product|
Concurrent::Future.execute({executor: thread_pool}) do
p "processing #{product.id}"
end
@glaucocustodio
glaucocustodio / normalization.rb
Last active June 11, 2021 16:57
Implements 3 types of normalization in ruby
View normalization.rb
class Array
# Few types of normalizations
# https://developers.google.com/machine-learning/data-prep/transform/normalization
def normalize
return if self.empty?
x_min, x_max = self.minmax
dx = (x_max-x_min).to_f
@glaucocustodio
glaucocustodio / client.rb
Last active December 29, 2020 17:20
Fire and forget http request with em-http-request ruby gem
View client.rb
# You just need to set inactivity_timeout = 0.1 (or any other low number except zero).
# It will trigger the request and wait only 0.1 second for the response. You don't need to define the callbacks as well.
# Example code below:
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem "em-http-request"
end
@glaucocustodio
glaucocustodio / br_money_type.rb
Last active September 4, 2018 21:58
brazilian currency formating (Ruby on Rails - attributes api)
View br_money_type.rb
# app/types/br_money_type.rb
class BrMoneyType < ActiveRecord::Type::Decimal
def cast(value)
value.is_a?(String) ? value.clean_money : value
end
def deserialize(value)
value ? value.to_f : value
end
end
@glaucocustodio
glaucocustodio / hash_based.rb
Last active April 20, 2018 18:01
Surrealist gem extension
View hash_based.rb
class HashBased
include Surrealist
extend Schema
attr_reader :hash
def initialize(hash = {})
@hash = hash.deep_symbolize_keys
end
@glaucocustodio
glaucocustodio / cnpj_validator.swift
Last active February 4, 2016 17:19
Swift (1.2) function to validate CNPJ
View cnpj_validator.swift
// Returns true if the given cnpj is valid
func checkIfCNPJIsValid(cnpj: String) -> Bool {
if(cnpj.isEmpty) {
return true
}
// validates format, allowed ones: 38.041.242/0001-04, 38041242000104
var formatRegex = NSRegularExpression(pattern: "^[0-9\\.\\-\\/]*[0-9]$", options: NSRegularExpressionOptions.CaseInsensitive, error: nil)
@glaucocustodio
glaucocustodio / application_helper.rb
Created November 23, 2015 17:21
Rails helper that returns if current time is contained in a time range
View application_helper.rb
def current_time_between start_time, end_time
now = Time.now
start_time = start_time.split(':')
end_time = end_time.split(':')
start_time_hour = start_time.first.to_i
start_time_min = start_time.last.to_i
end_time_hour = end_time.first.to_i
end_time_min = end_time.last.to_i
@glaucocustodio
glaucocustodio / excel.pmt.formula.swift
Created May 28, 2015 18:16
Excel PMT Formula in Swift
View excel.pmt.formula.swift
class ExcelFormulas {
class func pmt(rate : Double, nper : Double, pv : Double, fv : Double = 0, type : Double = 0) -> Double {
return ((-pv * pvif(rate, nper: nper) - fv) / ((1.0 + rate * type) * fvifa(rate, nper: nper)))
}
class func pow1pm1(x : Double, y : Double) -> Double {
return (x <= -1) ? pow((1 + x), y) - 1 : exp(y * log(1.0 + x)) - 1
}
class func pow1p(x : Double, y : Double) -> Double {
@glaucocustodio
glaucocustodio / rails-4-2-unnest-query-in-array-columns.rb
Last active August 29, 2015 14:13
Scenario from issue with the use of postgres's unnest query in array columns on rails 4.2
View rails-4-2-unnest-query-in-array-columns.rb
# Activate the gem you are reporting the issue against.
# gem 'activerecord', '4.1.9' # test passing
gem 'activerecord', '4.2.0' # test failing
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)