Skip to content

Instantly share code, notes, and snippets.

@dodecaphonic
dodecaphonic / rio.rb
Last active January 30, 2022 22:33
Trying to encode a RIO-like thing in Ruby's RBS
class RIO
def self.succeed_now(value)
Succeed.new(value)
end
def self.fail_now(error)
Fail.new(error)
end
def flat_map(&blk)
@dodecaphonic
dodecaphonic / intersection_filter.rb
Last active September 9, 2016 15:44
Intersection Filter
require "minitest/autorun"
class TestIntersectionFilter < Minitest::Test
def test_data
[[1, 2, 3], [1, 2, 4], [1, 2, 5],
[1, 5, 7], [1, 5, 8], [1, 5, 9]]
end
def test_that_it_works
filtered_data = IntersectionFilter.filter(test_data)
https://lyncwebsp.softtek.com/lwa/WebPages/LwaClient.aspx?legacy=RmFsc2U!&xml=PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48Y29uZi1pbmZvIHhtbG5zOnhzZD0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEiIHhtbG5zOnhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEtaW5zdGFuY2UiIHhtbG5zPSJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3J0Yy8yMDA5LzA1L3NpbXBsZWpvaW5jb25mZG9jIj48Y29uZi11cmk-c2lwOmNhcmxvcy52YXJnYXNAc29mdHRlay5jb207Z3J1dTtvcGFxdWU9YXBwOmNvbmY6Zm9jdXM6aWQ6UThVR0xKRzI8L2NvbmYtdXJpPjxzZXJ2ZXItdGltZT4xMzkuMDk5NDwvc2VydmVyLXRpbWU-PG9yaWdpbmFsLWluY29taW5nLXVybD5odHRwczovL21lZXRzcC5zb2Z0dGVrLmNvbS9jYXJsb3MudmFyZ2FzL1E4VUdMSkcyPC9vcmlnaW5hbC1pbmNvbWluZy11cmw-PGNvbmYta2V5PlE4VUdMSkcyPC9jb25mLWtleT48ZmFsbGJhY2stdXJsPmh0dHBzOi8vbWVldHNwLnNvZnR0ZWsuY29tL2Nhcmxvcy52YXJnYXMvUThVR0xKRzI_c2w9PC9mYWxsYmFjay11cmw-PHVjd2EtdXJsPmh0dHBzOi8vbHluY3dlYnNwLnNvZnR0ZWsuY29tL3Vjd2EvdjEvYXBwbGljYXRpb25zPC91Y3dhLXVybD48dWN3YS1leHQtdXJsPmh0dHBzOi8vbHluY3dlYnNwLnNvZnR0ZWsuY29tL3Vjd2EvdjEvYXBwbGljYXRpb25zPC91Y3dhLWV4dC11cmw-PHVjd2EtaW50LXVybD
require "rgeo"
WGS84 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
WGS84_UTM = "+proj=utm +zone=22 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
x0, y0 = -48.10311146131618, -15.894902327065926
x1, y1 = -48.103022948419394, -15.894858472899413
mercator = RGeo::Geographic.simple_mercator_factory
geographic = RGeo::Geographic.projected_factory(projection_proj4: WGS84)
@dodecaphonic
dodecaphonic / Gemfile
Created April 2, 2014 13:26
Exploring QuickCheck in Ruby via Rantly
source "https://rubygems.org"
gem "minitest"
gem "rantly", github: "hayeah/rantly"
@dodecaphonic
dodecaphonic / no_prying_when_commiting.sh
Last active December 18, 2015 21:49
git pre-commit hook to avoid committing pry or debugger inspections.
#!/bin/sh
for FILE in `git diff-index --name-status HEAD -- | awk '{print $2}'`; do
if [ "echo $FILE | grep .rb" ]; then
if [ "grep 'binding.pry|debugger' $FILE" ]; then
echo "$FILE: pry or debugger call. Fix it before committing."
exit 1
fi
fi
done
require 'minitest/autorun'
TowEvent = Struct.new(:id, :license_plate, :category, :operator, :tow_car,
:entry_time, :address, :ordering)
class OrdersTowEvents
WEIGHTS = { truck: 100, car: 200, bike: 300 }
attr_reader :grouped_events
private :grouped_events
@dodecaphonic
dodecaphonic / delayable_mailer.rb
Last active December 16, 2015 20:49
A very-much-in-need-of-improvement way to replace DelayedJob with SuckerPunch for your application mailers.
class DelayableMailer < ActionMailer::Base
class QueueSetupError < StandardError; end
class << self
def queue(options)
@queue_name = options.fetch(:name)
@workers = options.fetch(:workers) { 2 }
setup_queue
end
class MiopicAPI
def do_something_with(foo_id, bar_id)
foo = Foo.find(foo_id)
bar = Bar.find(bar_id)
# ...
end
end
# Refactoring 1
@dodecaphonic
dodecaphonic / gist:4203226
Created December 4, 2012 12:18
Drawing on canvas and getting an URI
class Smile.Utils.MarkImageDrawer
constructor: (@mark, @canvas) ->
@margin = 4
@backgroundColor = '#338e07'
@foregroundColor = '#fff'
@width = 40
@height = 60
# Public: Draws a Mark in the buffer.
#