Skip to content

Instantly share code, notes, and snippets.

View alebian's full-sized avatar
🌊

Alejandro Bezdjian alebian

🌊
View GitHub Profile
const sublistsOf = (list, number) => {
const result = [];
let aux = [];
list.forEach(el => {
if (aux.length >= number) {
result.push(aux);
aux = [];
}
aux.push(el);
@alebian
alebian / result.rb
Created June 23, 2020 01:30
Result object to use in case statements similar to Elixirs pattern matching for standard library
class Result
def initialize(options = {})
@success = options[:success] || false
@error = options[:error]
@result = options[:result]
end
def success?
@success
end
# Helper classes
class String
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
colorize(31)
end
class BaseCommand
def initialize(params: {}, context: {})
@params = params
@context = context
end
def call; end
def undo; end
end
@alebian
alebian / pascal_math.rb
Last active September 7, 2019 14:03
Pascal's triangle implementation
require_relative 'pascal_triangle'
class PascalMath
def initialize
@triangle = PascalTriangle.new
end
def binomial_power(a, b, n)
coefficients = @triangle.get_file(n)
@alebian
alebian / 20190525221739_change_version_type.rb
Created May 26, 2019 15:37
Change schema version type of a Rails app to use the DB by a Phoenix app
class ChangeVersionType < ActiveRecord::Migration[5.2]
def up
add_column :schema_migrations, :version2, :string
versions = ActiveRecord::Base.connection.execute('SELECT * FROM schema_migrations').to_a
versions.each do |version|
ActiveRecord::Base.connection.execute("UPDATE schema_migrations SET version2 = '#{version['version']}' WHERE version = '#{version['version']}'")
end
remove_column :schema_migrations, :version, :string
#!/bin/sh
mkdir .githooks
curl https://gist.githubusercontent.com/alebian/d7b3e18549599cacec3f16621c67e6f5/raw/pre-push.sh > .githooks/pre-push
chmod u+x .githooks/pre-push
git config core.hooksPath ./.githooks
#!/usr/bin/env ruby
def checkout_to_master_and_delete_current_branch
current_branch = `git rev-parse --abbrev-ref HEAD`.strip
`git checkout master`
puts "Deleting branch #{current_branch}"
`git branch -D #{current_branch}`
puts "Fetching master"
`git fetch -p`
puts "Pulling master"
class DatabaseRowStream
include Enumerable
BATCH_SIZE = 20_000
def initialize(sql, options = {})
@sql = sql
if options[:pluck]
@pluck = options[:pluck].respond_to?(:join) ? options[:pluck].join(', ') : options[:pluck]
end
class Retryer
def initialize
@retry_times = 3
@block = Proc.new {}
@args = nil
@backoff = false
end
def block(&block)
@block = block