Skip to content

Instantly share code, notes, and snippets.

View inopinatus's full-sized avatar
🐼
fuzzy logic

Josh Goodall inopinatus

🐼
fuzzy logic
View GitHub Profile
@inopinatus
inopinatus / ackermann.rb
Created June 1, 2018 11:38
Ackermann function in one line of Ruby as a hash default proc
A = Hash.new { |a,(m,n)| a[[m,n]] = m==0 ? n+1 : n==0 ? a[[m-1,1]] : a[[m-1, a[[m, n-1]]]] } #=> {}
A[[3,4]] #=> 125
A.inspect #=> ... long
@inopinatus
inopinatus / fibers_with_blocks.rb
Created June 2, 2018 02:56
Block yielding to a block, via fibers.
# importer/exporter interaction via fibers
require 'fiber'
class Importer
DATA = [
["foo", "bar"],
["jazz", "quux"],
["cats", "dogs"]
]
@inopinatus
inopinatus / gist:4e936864ab3d5b79b8f050bce39b3667
Created June 16, 2018 08:48 — forked from steveclarke/gist:1411146
Git: Setting up a Remote Repository and Doing Initial Push

Setup remote repository:

ssh git@example.com
mkdir my_project.git
cd my_project.git
git init --bare

On local machine:

cd my_project

@inopinatus
inopinatus / attachable_sti_type.rb
Last active June 19, 2018 04:03
Remapping type column values using the Rails 5 Attributes API, good for both STI and polymorphic belongs_to
# lib/attachable_sti_type.rb
class AttachableStiType < ActiveRecord::Type::String
def cast_value(value)
if value =~ /\A([a-z]+)\/([a-z]+)\Z/
"#{$2}/#{$1}_record".classify
else
super
end
end
@inopinatus
inopinatus / unique_collected_validator.rb
Last active July 12, 2018 05:32
Solve unique validation problem within nested simultaneous destruction and creation
class UniqueCollectedValidator < ActiveRecord::Validations::UniquenessValidator
def initialize(options)
unless options[:within].present?
raise ArgumentError, ":within option is required to determine the unique scope's collection."
end
unless Array(options[:within]).all? { |within| within.respond_to?(:to_sym) }
raise ArgumentError, "#{options[:within]} is not a supported format for the :within option. " \
"Pass a symbol or an array of symbols instead: `within: :profiles`"
end
super
@inopinatus
inopinatus / hash_proc_lookup.rb
Created June 21, 2018 05:35
Cheap lookups with Hash#to_proc
[:foo, :bar].map(&{ bar: "BAZ", foo: "QUUX" })
@inopinatus
inopinatus / invoice.rb
Last active June 25, 2018 10:46
json models embedded in your activerecord models
class Invoice < ApplicationRecord
include JsonModel::Attribute
json_attribute_model :payment, Payment
validates_associated :payment
end
@inopinatus
inopinatus / simpsonian.rb
Last active June 28, 2019 00:26
ruby's little-known array parameter destructuring.
def list((head, *tail))
case tail.length
when 0
head&.fetch(:name).to_s
when 1
"#{list [head]} & #{list tail}"
else
"#{list [head]}, #{list tail}"
end
end
@inopinatus
inopinatus / verify_and_decrypt_session_cookie52.rb
Created July 29, 2018 10:12
Decrypt Rails 5.2 session cookies
require 'cgi'
require 'active_support'
def verify_and_decrypt_session_cookie(cookie, secret_key_base = Rails.application.secret_key_base)
cookie = CGI::unescape(cookie)
salt = 'authenticated encrypted cookie'
encrypted_cookie_cipher = 'aes-256-gcm'
serializer = ActiveSupport::MessageEncryptor::NullSerializer
key_generator = ActiveSupport::KeyGenerator.new(secret_key_base, iterations: 1000)
@inopinatus
inopinatus / table_to_csv.rb
Last active June 28, 2019 00:23 — forked from sandys/table_to_csv.rb
convert a html table to CSV using ruby
require 'rubygems'
require 'nokogiri'
require 'csv'
f = File.open("table.html")
doc = Nokogiri::HTML(f)
CSV.open("output.csv", 'w') do |csv|
doc.xpath('//table/tbody/tr').each do |row|
tarray = []