Skip to content

Instantly share code, notes, and snippets.

@cyrilchampier
cyrilchampier / include_example.rb
Created November 29, 2019 15:16
Module include: Real world example
module Logging
def log(level, message)
File.open("log.txt", "a") { |f| f.write "#{level}: #{message}" }
end
end
class Service
include Logging
def do_something
@cyrilchampier
cyrilchampier / test.rb
Created November 27, 2019 15:00
singleton class
# Class
class Vehicule
def move
'I move'
end
end
# Singleton Class
class << Vehicule
def available_colors
@cyrilchampier
cyrilchampier / test.rb
Created November 27, 2019 14:59
include/extend/prepend simple
module KlassIncluded
def truc
puts "KlassIncluded::truc"
super
end
end
module KlassExtended
def truc
puts "KlassExtended::truc"
@cyrilchampier
cyrilchampier / test.rb
Created November 27, 2019 14:59
extend == singleton include
# extend == include in singleton class
module Mincluded
def toto
puts 'Mincluded#toto'
end
end
module MSingletonIncluded
def toto
@cyrilchampier
cyrilchampier / test.rb
Created November 27, 2019 14:58
include/extend/prepend
module Mincluded
def toto
puts 'Mincluded#toto'
end
module Mprepended
def toto
puts 'Mprepended#toto'
end
end
module KlassIncluded
def truc
puts "KlassIncluded::truc"
super
end
end
module KlassExtended
def truc
puts "KlassExtended::truc"
@cyrilchampier
cyrilchampier / database_randomizer.rb
Created July 26, 2019 07:51
Patch on ActiveRecord to randomize all select without order
# frozen_string_literal: true
# As PostgreSQL documentation states:
# https://www.postgresql.org/docs/9.1/queries-order.html
# The actual order in that case will depend on the scan and join plan types and the order on disk,
# but it must not be relied on.
#
# Problem is, in 99% of the CI run, in tests, the order will be the same.
# And in 1% of the run, regardless how many retries, the order will be different and the test will fail.
# Solution here is to "really" randomize requests so that retries will work,
# From https://github.com/doctolib/doctolib/blob/144688fe1aec739ffc6fcf7e6cd30a9d24759f82/test/integration/doctor/desktop/agenda/appointment/modal/booking1_test.rb#L254
# full inherited setup
describe 'not described in fact' do
setup do
@agenda = create :agenda, agenda_options
@account = create :account, account_options.merge(kind: Account::Kind::DOCTOR, job: Account::Job::PRACTITIONER)
agenda_authorization = create :agenda_authorization, account: account, agenda: agenda
@account.update agenda_preferences: @account.agenda_preferences.merge(splitCalendarWeekColumns: false)
@date = Time.zone.parse('19th Oct 2013 3:00pm')
@date = Time.zone.parse('20th Oct 2016 10:00am')
@cyrilchampier
cyrilchampier / retry_mechanism.rb
Created December 20, 2018 16:06
minitest override for flaky retry
def run
try_results = []
loop do
try_results << super
if self.class.disable_retry || try_results.count >= MAX_TRIES || try_results.last.failure.nil?
break
else
reset_state
end
end
@cyrilchampier
cyrilchampier / modify_const.rb
Created December 19, 2018 16:53
exemple of mutated const
module A
MY_CONST = 'my_value'
def self.func1
func2(MY_CONST)
MY_CONST == 'my_value'
end
def self.func2(string)
string.next!