Skip to content

Instantly share code, notes, and snippets.

Avatar
🐼
fuzzy logic

Josh Goodall inopinatus

🐼
fuzzy logic
View GitHub Profile
View digest_queries.rb
module DigestQueries
def predicate_builder
@predicate_builder ||= super.tap do |pb|
pb.register_handler(Digest::Instance, DigestHandler.new(pb))
end
end
class DigestHandler
def initialize(predicate_builder)
@predicate_builder = predicate_builder
@inopinatus
inopinatus / frequency_extension.rb
Last active May 10, 2020
arel "most frequent"
View frequency_extension.rb
module FrequencyExtension
def most_frequent
frequency_order.take
end
def frequency_order
group(arel_attribute(primary_key).order(arel_attribute(primary_key).count.desc)
end
end
View yfib.rb
y = ->(f) {
->(x) { x.(x) }.(
->(x) { f.(->(v) { x.(x).(v) }) } )
}
fib = y.(->(f) {
->(n) { n < 2 ? Array(0..n) : f[n-1].then { _1 << _1[-1] + _1[-2] } }
})
fib[6] #=> [0, 1, 1, 2, 3, 5, 8]
@inopinatus
inopinatus / AllowDescribeNetworkInterfacesAndAssignIpv6Address
Last active Jan 26, 2020
OpWorks Stacks managed instance self-update to have IPv6 address.
View AllowDescribeNetworkInterfacesAndAssignIpv6Address
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:DescribeNetworkInterfaces",
"ec2:AssignIpv6Addresses"
],
@inopinatus
inopinatus / all_year.rb
Created Jan 22, 2020
ActiveSupport's Date#all_* ranges inadvertently exclude times that should match.
View all_year.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails", github: "rails/rails"
gem "sqlite3"
end
require "active_record"
@inopinatus
inopinatus / relational_recommendations.rb
Last active Oct 9, 2019
Fun with relational composition in Rails
View relational_recommendations.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile true, quiet: true do
source "https://rubygems.org"
gem "rails", "6.0.0"
gem "sqlite3"
gem "pry"
end
View array_fold.rb
class Array
class << self
def fold((head, *tail), &block)
block::(head, *tail)
end
end
def fold(&block)
self.class.fold(self, &block)
end
@inopinatus
inopinatus / offer.rb
Last active Sep 8, 2019
Discount offer
View offer.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby "~> 2.7"
gem "rails", github: "rails/rails"
gem "sqlite3"
@inopinatus
inopinatus / liquid_markdown.rb
Created Sep 7, 2019
Simple renderer for Markdown -> Liquid templates
View liquid_markdown.rb
# frozen_string_literal: true
require 'action_view/template'
module LiquidMarkdown
class Handler
cattr_accessor :markdown_options, default: { autolink: true, tables: true, no_intra_emphasis: true }
cattr_accessor :strict_liquid, default: true
def self.call(template, source = nil)
@inopinatus
inopinatus / bundlemark.sh
Last active Jul 17, 2019
crude little benchmark for parallelizing bundle-installed gems
View bundlemark.sh
#!/bin/bash
TMPDIR="$(mktemp -d)"
CORES=$(sysctl -n hw.ncpu 2>/dev/null || nproc)
echo "==> testing with $CORES cores in $TMPDIR"
cd $TMPDIR
RUBY_VERSION=$(rbenv version-name)
TIME=$(which time)