Skip to content

Instantly share code, notes, and snippets.

View mberlanda's full-sized avatar

Mauro Berlanda mberlanda

  • Amazon
  • Madrid
  • 23:01 (UTC +02:00)
View GitHub Profile
@mberlanda
mberlanda / song_representer_spec.rb
Last active June 15, 2016 15:38
Parsing JSONAPI using representable gem in Ruby on Rails 4
require 'rails_helper'
require 'representable/json'
require 'representable/json/collection'
class Song < OpenStruct
end
# Ex. 1 using standalone collections
class SongRepresenter < Representable::Decorator
include Representable::JSON
@mberlanda
mberlanda / quick_sort.rb
Created July 21, 2016 22:00
Quicksort Algorithm Ruby Functional
# source: https://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Ruby
class Array
def quick_sort
h, *t = self
h ? t.partition { |e| e < h }.inject { |l, r| l.quick_sort + [h] + r.quick_sort } : []
end
end
@mberlanda
mberlanda / vat_number.rb
Created July 27, 2016 12:49
Partita IVA (Italian VAT identification number) checksum calculation and validation in Ruby
# https://it.wikipedia.org/wiki/Partita_IVA#Struttura_della_partita_IVA
module ChecksumCalculation
def calculate(ary)
(10 - t(ary)) % 10
end
private
def t(ary)
odd, even = separate_odd_even(ary)
@mberlanda
mberlanda / newtons_method.rb
Created March 24, 2017 09:33
Ruby implementation of the Newton's method (https://en.wikipedia.org/wiki/Newton%27s_method)
class NewtonsMethod
def initialize(f_proc, f_prime_proc)
@fp = f_proc
@fpp = f_prime_proc
end
def apply(x)
x - (@fp.call(x) / (@fpp.call(x) * 1.0))
end
@mberlanda
mberlanda / riemann_sum.rb
Last active April 5, 2017 15:58
Ruby Implementation of Riemann Sum (left and right solution)
# https://en.wikipedia.org/wiki/Riemann_sum
class RiemannSum
def initialize(f, v_start, v_end, n_step)
@f = f
@v_start = v_start
@v_end = v_end
@n_step = n_step
@step_size = (v_end - v_start) / (n_step * 1.0)
end
@mberlanda
mberlanda / rails_assets_coverage.pl
Last active September 1, 2017 12:46
The purpose of this script is to find which assets are used by a Rails app. I tried to not use any dependency in order to make it runnable on every UNIX machine. This should be intended as a proof of concept for a future ruby gem.
#!/usr/bin/perl
use strict;
use warnings;
use 5.014;
=head1 NAME
Rails Assets Coverage
@mberlanda
mberlanda / gemfile_parser.rb
Created October 5, 2017 16:06
Run it as `$ ruby gemfile_parser.rb > Gemfile ` to generate a new Gemfile from a Gemfile.lock
require 'bundler'
lockfile = Bundler::LockfileParser.new(Bundler.read_file('Gemfile.lock'))
specs = lockfile.specs
gems_hash = Hash.new.tap do |h|
specs.each do |s|
h[s.name] = {
spec: s,
dependencies: s.dependencies.map(&:name)

Keybase proof

I hereby claim:

  • I am mberlanda on github.
  • I am mberlanda (https://keybase.io/mberlanda) on keybase.
  • I have a public key ASAmHgHzX-F6f6Q2QgGnrbqQLRJfeaXgCw54YdMeRyJuQwo

To claim this, I am signing this object:

@mberlanda
mberlanda / README.md
Last active July 16, 2018 16:14
Allow to install multiple gem versions

Multiple gem versions

While developing a serialization gem I needed to compare my current code with previous gem versions.

(Un)fortunately bundler does not allow to install several versions of the same gem to prevent conflicts on class names. The purpose of this script is to wrap a gem into a namespace/module named.

You should set in the main script:

  • L7 TARGET_DIR: the dir where the gems will be extracted
  • L8 PKG_DIR : the dir where the .gem files will be downloaded
@mberlanda
mberlanda / rebase_branches.sh
Created July 26, 2018 15:38
Rebase several branches
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
PROJECT_DIR=$(git rev-parse --show-toplevel)
# Base branch for rebasing
BASE_BRANCH="master"
# Enter here the name of the branches to rebase