Skip to content

Instantly share code, notes, and snippets.

View mberlanda's full-sized avatar

Mauro Berlanda mberlanda

View GitHub Profile
@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)
@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 / 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 / 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 / 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 / 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 / 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