Skip to content

Instantly share code, notes, and snippets.

Avatar
🌊

Alejandro Bezdjian alebian

🌊
View GitHub Profile
View sublist.js
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 Jun 23, 2020
Result object to use in case statements similar to Elixirs pattern matching for standard library
View result.rb
class Result
def initialize(options = {})
@success = options[:success] || false
@error = options[:error]
@result = options[:result]
end
def success?
@success
end
View .irbrc
# Helper classes
class String
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
colorize(31)
end
View base_command.rb
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 Sep 7, 2019
Pascal's triangle implementation
View pascal_math.rb
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
Change schema version type of a Rails app to use the DB by a Phoenix app
View 20190525221739_change_version_type.rb
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
View hook-installation-in-another-folder.sh
#!/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
View scripts.rb
#!/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"
View database_row_stream.rb
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
View retryer.rb
class Retryer
def initialize
@retry_times = 3
@block = Proc.new {}
@args = nil
@backoff = false
end
def block(&block)
@block = block