Skip to content

Instantly share code, notes, and snippets.

View baweaver's full-sized avatar
📝
Documenting things

Brandon Weaver baweaver

📝
Documenting things
View GitHub Profile
class Cond
IDENTITY = -> v { v }
def initialize(if_cond: IDENTITY, then_branch: IDENTITY, else_branch: IDENTITY)
@if_cond = if_cond
@then_branch = then_branch
@else_branch = else_branch
end
def call(v)
# Bad - Hard to read
some.really.long.chain_of_things(:with,
:arguments,
:that,
:are,
:out_here,
:is_unreadable,
:at_a_glance)
# Good - Eye doesn't flow far right then
KEYBOARD = [
'1234567890-='.chars,
'qwertyuiop[]'.chars,
'asdfghjkl;\''.chars,
'zxcvbnm,./'.chars,
]
def reasonable_typos(distance: 3)
typo_map = Hash.new do |h, initial_key|
h[initial_key] = {}
@baweaver
baweaver / deep_group.rb
Created August 2, 2022 05:53
Deep grouping, because I keep doing something like this by hand and it gets hard to keep track of.
module DeepGroup
class Grouping
IDENTITY = -> v { v }
def initialize(state)
@state = state
@groupings = []
@mapping = IDENTITY
end
module AccessedAttributes
def self.included(klass)
association_proxy = Module.new do
def accessed_associations
@_accessed_associations ||= []
end
end
klass.reflect_on_all_associations.each do |association|
name = association.name
@baweaver
baweaver / range_string_include_triple_equals_bug.rb
Created October 24, 2022 04:52
Why does `include?` behave differently than `===` here?
range = '0'..'255'
('1'..'5').map do |v|
{
v: v,
teq: range === v,
include?: range.include?(v)
}
end
# => [
@baweaver
baweaver / tak_strategy.md
Created May 19, 2016 06:21
Strategies I've picked up from a few games of Tak

4 x 4 Strategy

Because of the lack of capstones and ease at which a piece-out can be achieved, 4x4 is best played as an effort towards the secondary win conditions. This is especially true of the second to play. Always play an open piece unless by doing so you will guarantee a loss. Avoid capturing, only place.

In doing so, your opponent will likely try to capture your pieces. Encourage this, and place stones to bait them into further captures.

Contengencies

In and of itself, this is also a contingency plan towards a primary win by poisoning and weakening their stones. A black cap on multiple white stones can be recaptured at an opportune time to turn the tide of the game in a few well timed moves. This is especially true if you lure their larger towers together, forcing them to relinquish control over any of your nearby pieces.

@baweaver
baweaver / rails_booklist.md
Last active September 15, 2022 17:53
Rails Booklist

Rails Book List

A list of Rails books and their applications. Free books are tagged with (F)

Have a suggestion? Leave a comment. There are still books I need to read on the subject so some may not show up in this list yet.

Learning Rails

Your first steps into Rails

IDENTITY = -> v { v }
def proc_line(**fns)
fns.reduce(IDENTITY) { |fn, (m, args)|
fn >> m.to_proc.then { |m_proc| -> v { m_proc.call(v, *args) } }
}
end
[50, 100, 300, 5000].map(&proc_line(
to_s: 2, chars: nil, reverse: nil, join: nil, to_i: 2
@baweaver
baweaver / quick_adt.rb
Last active May 24, 2022 07:34
Quick musing on ADT in Ruby using Sorbet, name TBD. May gemify it later.
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)] }