Skip to content

Instantly share code, notes, and snippets.

View rivsc's full-sized avatar

Claudel Sylvain rivsc

View GitHub Profile
@rivsc
rivsc / prawn_rollback_test.rb
Created November 14, 2011 15:23
Prawn #rollback method is broken
require 'rubygems'
require 'prawn'
Prawn::Document.generate("make_a_broken.pdf") do |pdf|
pdf.font_size 12
pdf.move_down 20
pdf.font "Helvetica"
# We use the rollback method so the text in this block should not be displayed
pdf.transaction do
class Test < ActiveRecord::Base
belongs_to :bidule
has_many :truc
end
require 'Benchmark'
def inject_method(arr)
arr.inject(Hash.new(0)) { |a, e| a[e] += 1 ; a }
end
def each_wo_method(arr)
arr.each_with_object(Hash.new(0)) { |e, a| a[e] += 1 }
end
@rivsc
rivsc / photoflash.rb
Last active September 13, 2018 06:02
Raspberrypi with custom flash light
#!/usr/bin/env ruby
# @rivsc / http://blog.escarworld.com
require 'rpi_gpio'
PIN_NUM = 12
RPi::GPIO.set_numbering :board
RPi::GPIO.setup PIN_NUM, :as => :output
@rivsc
rivsc / hash
Created June 14, 2017 13:50
Hash syntaxe
# Clé integer
2.3.3 :054 > { 4 => 'truc' }
=> {4=>"truc"}
2.3.3 :055 > { 4: 'truc' }
SyntaxError: (irb):55: syntax error
# Clé string
2.3.3 :061 > { 'cle': 'truc' }['cle']
@rivsc
rivsc / calcul_cle_rib.rb
Last active October 25, 2019 13:56
Méthode de calcul de la clé RIB (relevé d'identité bancaire)
# Methode de calcul de la clé RIB (relevé d'identité bancaire)
# le calcul vient de wikipedia fr
def cle_rib(code_banque, code_guichet, numero_compte)
mapping = ["", "AJ", "BKS", "CLT", "DMU", "ENV", "FOW", "GPX", "HQY", "IRZ"]
numero_compte.scan(/[A-Z]/).each do |ch|
mapping.each_with_index do |str, i|
if str.include?(ch)
numero_compte = numero_compte.gsub(ch, i.to_s)
break
end
@rivsc
rivsc / calcul_cle_rib.js
Created October 25, 2019 14:41
Calcul clé rib javascript
// Fonction de calcul de la clé RIB en javascript
function cle_rib(code_banque, code_guichet, numero_compte){
var mapping = ["", "AJ", "BKS", "CLT", "DMU", "ENV", "FOW", "GPX", "HQY", "IRZ"];
var cherche_lettre = new RegExp(/[A-Z]/g);
var res = numero_compte.match(cherche_lettre);
if(res.length > 0){
res.forEach(function(ch){
mapping.forEach(function(str, i){
if(str.includes(ch)){
numero_compte = numero_compte.replace(new RegExp(ch,"g"), i);