Skip to content

Instantly share code, notes, and snippets.

View alainravet's full-sized avatar

Alain Ravet alainravet

  • Braine l'Alleud, Belgium
View GitHub Profile
@alainravet
alainravet / build_git_example.sh
Created July 29, 2016 07:45
git merge vs rebase+merge sample generation
slip() {
sleep 1
}
rm -rf contents
mkdir contents
cd contents
git init
class Fixnum
def to_roman
[].tap do |res|
a, b = self.divmod(10) ; res << ([''] + %w(I II III IV V VI VII VIII IX))[b]
a, b = a.divmod(10) ; res << ([''] + %w(X XX XXX XL L LX LXX LXXX XC))[b]
a, b = a.divmod(10) ; res << ([''] + %w(C CC CCC CD D DC DCC DCCC CM))[b]
a, b = a.divmod(10) ; res << ([''] + %w(M MM MMM Mv v vM vMM vMMM Mx))[b]
a, b = a.divmod(10) ; res << ([''] + %w(x xx xxx xl l lx lxx lxxx xc))[b]
a, b = a.divmod(10) ; res << ([''] + %w(c cc ccc cd d dc dcc dccc cm))[b]
end.reverse.join
@alainravet
alainravet / regexp_parser.rb
Created January 15, 2016 14:03
a basic Regexp-based parser
# what: a Regexp-based parser. alainravet - 2016 jan
SYNTAX_RULES = {
/^PRINT\b/ => {
/^PRINT\s*\z/ => :new_line_command,
/^PRINT (.*)/ => :print_command,
},
/HELP/i => :help_command,
}
@alainravet
alainravet / hash-diff.rb
Created October 15, 2015 06:43
how to diff 2 hashes
class Hash
def diff(other)
(self.keys + other.keys).uniq.inject({}) do |memo, key|
unless self[key] == other[key]
if self[key].kind_of?(Hash) && other[key].kind_of?(Hash)
memo[key] = self[key].diff(other[key])
else
memo[key] = [self[key], other[key]]
end
end
@alainravet
alainravet / RPM calculator.rb
Last active August 29, 2015 14:21
a RPN calculator in 19 lines of Ruby
def RPN(*data)
stack = []
data.each do |el|
case el
when :+, :-, :*, :/, :&, :|, :<< # 1,2,3,:*,:+ == 1 + 2 $ 3
a, b = stack.pop(2)
stack.push a.send(el.to_sym, b)
when Class # 3,Float == Float(3)
stack.push send(el.name, stack.pop)
when Symbol # '1+2',:eval == eval('1+2')
@alainravet
alainravet / service object with on_success and on_failure hooks.rb
Last active August 29, 2015 14:20
Service Object with on_success and on_failure blocks
#----------------------
# Library code:
class BasicServiceObject
attr_reader :object
def initialize(o)
@object = o
end
end
require 'active_record'
require 'database_cleaner'
connection_info = YAML.load_file("config/database.yml")["test"]
ActiveRecord::Base.establish_connection(connection_info)
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
module DefaultValues
def has_default_values(default_values = {})
cattr_accessor :default_values
self.default_values = default_values
after_initialize :assign_default_values
include InstanceMethods
# TODO :
# 1 : return the attributes that are missing
# 2 : in views, also test presence of documents
#
require_relative 'completeness_measure'
require 'minitest/spec'
require 'minitest/autorun'
describe CompletenessMeasure do
@alainravet
alainravet / env_vars_checker.rb
Last active January 1, 2016 07:39
mini DSL to validate environment variables presence and value
# Usage :
# EnvVarsChecker.new do
# env_includes 'USER'
# env_includes 'USER', in: -> { `echo $USER`.chomp}
# env_includes 'USER', in: %w(deployer deploy rails)
# end
class EnvVarsChecker
class Error < RuntimeError ; end