Skip to content

Instantly share code, notes, and snippets.

Avatar
📝
Documenting things

Brandon Weaver baweaver

📝
Documenting things
View GitHub Profile
@baweaver
baweaver / deep_group.rb
Created Aug 2, 2022
Deep grouping, because I keep doing something like this by hand and it gets hard to keep track of.
View deep_group.rb
module DeepGroup
class Grouping
IDENTITY = -> v { v }
def initialize(state)
@state = state
@groupings = []
@mapping = IDENTITY
end
@baweaver
baweaver / evolution_of_ruby_programmer.md
Last active Jul 28, 2022
Very much work in progress, will amend this as I have time. Feel free to comment ideas.
View evolution_of_ruby_programmer.md
@baweaver
baweaver / quick_adt.rb
Last active May 24, 2022
Quick musing on ADT in Ruby using Sorbet, name TBD. May gemify it later.
View quick_adt.rb
require "sorbet-runtime"
module ADT
module DeconstructableSorbetStruct
def deconstruct
self.class.props.map { send(_1) }
end
def deconstruct_keys(keys)
return self.class.props.to_h { |k| [k, send(k)] }
@baweaver
baweaver / code_migration_hypothetical.rb
Created Mar 9, 2022
Experimentation on what a code migration syntax might look like, ideally
View code_migration_hypothetical.rb
module CodeMigrations
# Number isn't important, vague syntax, and not all
# of these are specifically Rails examples
version from: 4.0, to: 5.0
# The easiest case, literally A to B direct translation.
class FilterToAction < Migration
expect_translation(
"after_filter" => "after_action",
# ...snipped excess for example
@baweaver
baweaver / block_transform_ast.rb
Last active Jun 19, 2022
Pattern matching applied to ASTs, defining the transformation between shorthand and standard block format.
View block_transform_ast.rb
require "rubocop"
# Useful for debugging and seeing how the nodes deconstruct
def deep_deconstruct(node)
return node unless node.respond_to?(:deconstruct)
node.deconstruct.map { deep_deconstruct(_1) }
end
def block_to_shorthand?(a, b)
@baweaver
baweaver / tree_difference.rb
Last active Feb 16, 2022
Tree differences, naive initial version, need to see if there's a nicer way to approach this....
View tree_difference.rb
require "rubocop"
def tree_distance(object_a, object_b)
tree_distance_matrix(object_a, object_b).last.last
end
def tree_distance_matrix(object_a, object_b)
a_size, b_size = object_a.size, object_b.size
# Rows and columns ascend from 0 to size + 1
@baweaver
baweaver / proc_ast_sql.rb
Created Feb 3, 2022
Proc to AST applied to potential foundation for SQL hackery
View proc_ast_sql.rb
require 'proc_to_ast'
def where(&fn)
ast = fn.to_ast
source = fn.to_source
pp ast:, source:
end
where { age > 18 && occupation.downcase == 'plumber' }
@baweaver
baweaver / ruby_without_e.rb
Created Feb 2, 2022
I present to you a horror from the depths, programming Ruby without the letter "e"
View ruby_without_e.rb
c = '' << 101
SUB = /___/
subs = -> s { s.gsub(SUB, c) }
fn = "/tmp/runs.rb"
# Patch it in for fun
`#{c}cho 'alias run #{subs['___val']}' > #{fn}`
load fn
mm = <<~ABC
View 03_prp_local_test.rb
require 'benchmark/ips'
class TimeFilter
attr_reader :start, :finish
def initialize(start, finish)
@start = start
@finish = finish
end
@baweaver
baweaver / html_rb.rb
Last active Aug 3, 2021
Quick HTML builder idea
View html_rb.rb
module HTML
VALID_TAGS = %i(html p div span a ol ul li strong em table thead tr td th tbody article aside)
SELF_CLOSING_TAGS = %i(img br hr)
# Evaluates a block passed to it for HTML tags. This is done to isolate
# defined tag methods from the outside world, hence `instance_eval` to
# evaluate that block in the context of an HTML structure.
class Evaluator
attr_reader :contents