Skip to content

Instantly share code, notes, and snippets.

View marcandre's full-sized avatar

Marc-André Lafortune marcandre

View GitHub Profile
@marcandre
marcandre / ex_vcr_process.ex
Created April 17, 2023 20:23
Allows calling ExVCR's `start_cassette` / `stop_cassette` from any process
defmodule Bobby.ExVcrProcess do
@moduledoc """
This GenServer allows calling `start_cassette` / `stop_cassette` from any process
# Instead of
use_cassette("x") do
do_something
end
# can be written as (each line from any process):
@marcandre
marcandre / ractor_test.rb
Last active December 31, 2020 21:36
Ractor benchmark
#!/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"
end
# usage `ruby ractor_test.rb <work_length> <num_workers> <num_requests>
@marcandre
marcandre / timeout.rb
Created December 10, 2020 11:57
Timeout::wake
def Timeout.wake(secs)
Thread.handle_interrupt(Timeout::Error => :on_blocking) do
Timeout.timeout(secs) do
yield
end
end
end
N = 100
SECS = 0.1
@marcandre
marcandre / miller_rabin_bench.rb
Last active December 5, 2020 10:57
Miller Rabin prime test
<<~RESULT
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
# 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|
x.report('with_regexp') { REGEXP.match? str }
@marcandre
marcandre / Set.md
Last active August 24, 2020 18:50

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:

Set#join

@marcandre
marcandre / require_changer.rb
Created October 6, 2018 03:19
require -> require_relative
require 'pathname'
Pathname('./lib').children.select(&:directory?).map(&:basename).each do |name|
Dir["./lib/#{name}/**/*.rb"].each do |path|
begin
code = File.read(path)
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}}
end
File.write(path, code)
@marcandre
marcandre / # postgresql - 2013-06-18_03-25-11.txt
Created September 12, 2018 13:27
postgresql on macOS 10.11.6 - Homebrew build logs
Homebrew build logs for postgresql on macOS 10.11.6
Build date: 2013-06-18 03:25:11
@marcandre
marcandre / compare_reverse_sort.rb
Created May 18, 2013 14:28
Comparing how to do a reverse sort
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! }
end
@marcandre
marcandre / plupload_bug
Last active December 17, 2015 02:48
Show bug with plupload's html5 runtime with IE10 when using colorbox
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!-- Load plupload and all it's runtimes and finally the jQuery queue widget -->
<script type="text/javascript" src="http://www.plupload.com/plupload/js/plupload.full.js"></script>
<script type="text/javascript" src="http://www.jacklmoore.com/colorbox/jquery.colorbox.js"></script>
<script type="text/javascript">