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 / config_initializers_export_migrations_as_ddl.rb
Created June 3, 2012 01:18
have `rake db:migrate` export the matching DDL to a file in db/migrate
if $0.end_with?('/rake') && ARGV[0].start_with?('db:migrate') && Rails.env.development?
#require 'active_record/migration'
klass =
case (adapter_class = ActiveRecord::Base.connection.class.to_s)
when 'ActiveRecord::ConnectionAdapters::SQLite3Adapter'
require 'active_record/connection_adapters/sqlite_adapter'
ActiveRecord::ConnectionAdapters::SQLiteAdapter
when 'ActiveRecord::ConnectionAdapters::Mysql2Adapter'
require 'active_record/connection_adapters/mysql2_adapter'
# 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 / 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 / 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
@alainravet
alainravet / aync_preloader.rb
Last active December 17, 2015 09:59
async preloading cache with code rewriting
# Ad-hoc code (no library)
#
module GemUtils
def self.gem_names # version 1 : 1st call
GEM_NAMES_PRELOADER.join.value # **1 : wait and return this if the cache is not ready
end
private
# response to http://jumpstartlab.com/news/archives/2013/04/23/the-death-of-ifs
#-----------------
# Commands :
#-----------------
class Command
class Quit ; def execute ; exit end end
class Invalid ; def execute ; puts 'invalid command' end end
class Tweeting ; def execute ; puts "tweeting" end end
@alainravet
alainravet / binary_file_string_utils.rb
Last active December 14, 2015 04:49
extract POIs from a TomTom *.ov2 file (in Ruby)
module BinaryFileStringUtils
def byte_to_int
self.unpack('U').first
end
def little_endian_to_int
# source : http://stackoverflow.com/questions/5236059/unpack-signed-little-endian-in-ruby
arr, bits, num = self.unpack('V*'), 0, 0
arr.each do |int|
num += int << bits
@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