Clay Shentrup·Pinnedsimpler enums in typescripti often see typescript enums used as constraints for redux actions and the like. for example: enum ActionTypes { GO = 'GO', STOP = 'STOP', } export const goAction = createAction(ActionTypes.GO, (resolve) => { return (payload?: any) => resolve(payload); }); function funcThatAcceptsAnAction(action: ActionTypes) { ... } it would be far…1 min read1 min read
Clay Shentrup·Mar 8team structuresmost companies scale teams and “territory” (namely, codebases) in an unstructured, evolutionary, ad hoc way. i once watched a talk about scaling while staying agile, and it inspired me to illustrate this with a specific hypothetical structure. let’s say we have an organization which can have one or more services…2 min read2 min read
Clay Shentrup·Jun 8, 2022num-paths exerciseour challenge is to write a function to return the number of paths we can traverse from the upper-left corner of a grid to the lower-right corner. i’ve chosen ruby as my language. MAZE = [ [0, 0, 0], [0, 1, 0], [0, 0, 0], ] def num_paths(x, y…1 min read1 min read
Clay Shentrup·Apr 25, 2022ordered dependencies problemproblem: given a graph of dependencies, write a function to return an array of ordered dependencies. that is, no dependency comes before one of its dependencies. cycles should be detected and cause failure. DEPS = { a: %i[b c], c: %i[b d], d: %i[e], } def get_deps(deps, ancestors) deps.flat_map do |child| raise('cycle') if ancestors.include?(child) get_deps(DEPS.fetch(child, []), ancestors + [child]) + [child] end.uniq end puts get_deps(DEPS.keys, []).inspectRuby1 min readRuby1 min read
Clay Shentrup·Jan 2, 2022Better Rails Partial RenderingHere was my problem. I wanted to render a polymorphic array like this. = render(@animals) E.g. templates named lions/lion or tigers/tiger as the case may be for each element in the array. As you probably guessed, my application didn’t actually use big cats, but a biological hierarchy is clear for…Ruby On Rails3 min readRuby On Rails3 min read
Clay Shentrup·May 1, 2021Building Arrays with Conditional Elements in Rubydef users [ { name: 'bob', email: 'bob@example.com' }, *([name: 'alice', email: 'alice@abc.xyz'] if inspecting_users?), { name: 'eve', email: 'eve@nsa.gov' }, ] end If inspecting_users? returns false, the returned array will contain only the first and last elements.Ruby1 min readRuby1 min read
Clay Shentrup·Mar 31, 2021Stimulus.js Lifecycle CallbacksOne common mistake I see in Stimulus controllers is that people use the connect() callback to set up event handlers when they really should be using the initialize() callback. Let’s look at the descriptions of these callbacks. It’s useful to note here that connect() and disconnect() can be called an…Ruby On Rails1 min readRuby On Rails1 min read
Clay Shentrup·Dec 7, 2020My RuboCop configrequire: rubocop-rails AllCops: # TODO EnabledByDefault: true NewCops: enable SuggestExtensions: rubocop-rspec: false # Disagree with 90% of it. Style/MethodCallWithArgsParentheses: AllowParenthesesInMultilineCall: true Enabled: true IgnoreMacros: false Style/Documentation: Exclude: - app/controllers/**/* - app/policies/**/* - db/**/* # Worry…Ruby1 min readRuby1 min read
Clay Shentrup·Sep 6, 2020Enumerable in Go Using GenericsThe latest iteration of generics is amazing. Here’s a short proof of concept for a Ruby-like enumerable construct. package main import ( "fmt" "strings" ) func newSliceEnum[T comparable](s []T) enumerable[T] { return newEnum[T](sliceEnum[T](s)) } type sliceEnum[T comparable] []T func (se sliceEnum[T]) Each(f func(T)) { for _, v := range se { f(v) } } func newEnum[T comparable](e eacher[T]) enumerable[T] { return enumerable[T]{eacher: e} }Golang1 min readGolang1 min read
Clay Shentrup·Jul 28, 2020Avoid []It’s a Risky Tool with No Clear Benefit What Avoid calling hash[key]. Instead use hash.fetch(key) if the key is expected to always be present, or hash.fetch(key, nil) if it’s valid/expected for the key to be absent. Why The only benefit of using hash[key] is that it’s six characters shorter to type than…Ruby4 min readRuby4 min read