Skip to content

Instantly share code, notes, and snippets.


Marc-André Lafortune marcandre

View GitHub Profile
marcandre / ractor_test.rb
Last active Dec 31, 2020
Ractor benchmark
View ractor_test.rb
#!/usr/bin/env ruby
if ENV['B']
BuiltinRactor = Ractor
Object.send :remove_const, :Ractor
require 'backports/3.0.0/ractor'
puts "Using pure Ruby implementation"
# usage `ruby ractor_test.rb <work_length> <num_workers> <num_requests>
View timeout.rb
def Timeout.wake(secs)
Thread.handle_interrupt(Timeout::Error => :on_blocking) do
Timeout.timeout(secs) do
N = 100
SECS = 0.1
marcandre / miller_rabin_bench.rb
Last active Dec 5, 2020
Miller Rabin prime test
View miller_rabin_bench.rb
Testing 10..100
current: 389.3 i/s
faster_prime: 269.8 i/s - 1.44x (± 0.00) slower
miller_rabin: 226.5 i/s - 1.72x (± 0.00) slower
Testing 100..1000
current: 323.4 i/s
faster_prime: 254.4 i/s - 1.27x (± 0.00) slower
miller_rabin: 213.0 i/s - 1.52x (± 0.00) slower
View blank_bench.rb
# gem install 'benchmark-ips'
require 'benchmark/ips'
long = ' ' * 1_000_000 + 'x'
REGEXP = /\A[[:space:]]*\z/
[' ', 'hello', long].each do |str|
Benchmark.ips do |x|'with_regexp') { REGEXP.match? str }

Sets need ♥️

When looking at RuboCop's code, I noticed a big number of frozen arrays being used only to later call include? on them. This is O(n) instead of O(1).

Trying to convert them to Sets causes major compatibility issues, as well as very frustrating situations (See set.join and array + set) and where the fact that they are now Sets makes them much less efficient (See array & set).

Here are the improvements that would improve Sets:


marcandre / require_changer.rb
Created Oct 6, 2018
require -> require_relative
View require_changer.rb
require 'pathname'
Pathname('./lib') do |name|
Dir["./lib/#{name}/**/*.rb"].each do |path|
code =
code.gsub!(%r{^(\s*)require (['"])#{name}/([\w/]*)['"]}) do
relative_path = Pathname("./lib/#{name}/#{$3}.rb").relative_path_from(Pathname(path).dirname)
%Q{#{$1}require_relative #{$2}#{relative_path.to_s[0..-4]}#{$2}}
File.write(path, code)
marcandre / # postgresql - 2013-06-18_03-25-11.txt
Created Sep 12, 2018
postgresql on macOS 10.11.6 - Homebrew build logs
View # postgresql - 2013-06-18_03-25-11.txt
Homebrew build logs for postgresql on macOS 10.11.6
Build date: 2013-06-18 03:25:11
marcandre / compare_reverse_sort.rb
Created May 18, 2013
Comparing how to do a reverse sort
View compare_reverse_sort.rb
require 'fruity'
a = (1..100).to_a.shuffle
compare do
sort { a.sort{|x, y| y <=> x} }
sort_by { a.sort_by{|x| -x} }
reverse { a.sort.reverse }
reverse! { a.sort.reverse! }
marcandre / plupload_bug
Last active Dec 17, 2015
Show bug with plupload's html5 runtime with IE10 when using colorbox
View plupload_bug
<script type="text/javascript" src=""></script>
<!-- Load plupload and all it's runtimes and finally the jQuery queue widget -->
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
marcandre / make.log
Created Mar 12, 2013
more /Users/mal/.rvm/log/ruby-2.0.0-p0/make.log
View make.log
[2013-03-12 18:04:05] make
current path: /Users/mal/.rvm/src/ruby-2.0.0-p0
command(2): make -j2
CC = clang
LD = ld
LDSHARED = clang -dynamiclib
CFLAGS = -O3 -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Werror=pointer-arith -Werror=write-strings -Werror=declaration-after-statement -Werror=shorten-64-to-32 -Werror=implicit-function-declaration -fno-common -pipe
XCFLAGS = -include ruby/config.h -include ruby/missing.h -D_FORTIFY_SOURCE=2 -fstack-protector -fvisibility=hidden -DRUBY_EXPORT
CPPFLAGS = -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -I/usr/local/opt/libyaml/include -I/usr/local/opt/readline/include -I/usr/local/opt/libxml2/include -I/usr/local/opt/libxslt/include -I/usr/local/opt/libksba/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I. -I.ext/include/x86_64-darwin10.8.0 -I./include -I.