Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / Gemfile
Last active Apr 16, 2021
Testing GH JSON / JSON5 / YAML highlighting
View Gemfile
source 'https://rubygems.org'
gem 'json5'
@JoshCheek
JoshCheek / pry_wherever_exceptions_get_raised.rb
Last active Apr 15, 2021
Reproducing pry-exception_explorer
View pry_wherever_exceptions_get_raised.rb
require 'binding_of_caller'
require 'pry'
require 'pry-stack_explorer'
TracePoint.trace(:raise) do |tp|
next unless tp.path == __FILE__ # <-- correct for this example, not necessarily for your use case
# next unless tp.path.start_with? Rails.root.to_s # <-- might be what you want
tp.binding.pry
end
@JoshCheek
JoshCheek / active_record_sqlite_writable_views.rb
Last active Apr 14, 2021
Writable views to make denormalization less annoying
View active_record_sqlite_writable_views.rb
require 'active_record'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Base.connection.raw_connection.execute_batch <<~SQL
create table makes(id integer not null primary key, make text);
create table models(id integer not null primary key, model model);
create table normalized_cars(id integer not null primary key, make_id int, model_id int);
create view cars as
select c.id, makes.make, models.model
View TrixEditor.js
import React, { Component, Fragment } from 'react'
import PropTypes from 'prop-types'
const randomString = () =>
Math.random().toString(36).substring(2, 15)
export default class TrixEditor extends Component {
static propTypes = {
customRef: PropTypes.shape({ current: PropTypes.instanceOf(Element) }),
name: PropTypes.string,
@JoshCheek
JoshCheek / blog_code_feedback.md
Last active Apr 2, 2021
Refactoring + code review of some code I saw in a blog
View blog_code_feedback.md
@JoshCheek
JoshCheek / syncing_promises.js
Created Apr 1, 2021
JS syncing promises code sample
View syncing_promises.js
# https://twitter.com/josh_cheek/status/1377767258601181189
node -e '
const sleep = n => new Promise(resolve => setTimeout(resolve, n))
!async function() {
const start = Date.now()
const val1 = sleep(1000).then(() => console.log({ line: 5, at: Date.now()-start }))
const val2 = sleep(1500).then(() => console.log({ line: 6, at: Date.now()-start }))
const val3 = sleep(500 ).then(() => console.log({ line: 7, at: Date.now()-start }))
await Promise.all([val1, val2, val3]) // <-- IS THERE ANYTHING BETTER THAN THIS YET?!?!!?!?
@JoshCheek
JoshCheek / weak_memoization_through_rubys_weak_ref.rb
Last active Apr 1, 2021
Example of how you could use Ruby's WeakRef to perform weak memoization
View weak_memoization_through_rubys_weak_ref.rb
require 'weakref'
# This module allows methods to be memoized, but the memoized result can still be GC'd.
# So the object can live for a long time. Eg maybe it's created on server initialization
# and lasts for the lifetime of a server. If the calculated result is large,
# then memoizing it doesn't permanently allocate that memory.
module WeakMemo
def memoize(name)
method = instance_method name
ivar = :"@_memoized_#{name}"
@JoshCheek
JoshCheek / show_unused.rb
Created Mar 24, 2021
Show unused / untested stuff in Rails
View show_unused.rb
require 'set'
# For answering questions:
# * What views exist that we didn't render?
# * What controller actions exist that we didn't hit?
# * What routes exist that we didn't request? / generate a link to?
# * What files exist that we never required
class ShowUnused
def self.call(outstream)
data = {
@JoshCheek
JoshCheek / linked_list.py
Last active Mar 19, 2021
linked list in Python
View linked_list.py
import functools
class LList:
@classmethod
def fromArray(cls, array):
return functools.reduce(lambda ll, val: ll.append(val), array, cls())
def __init__(self): self.isEmpty = True
def append(self, val): return LNode(val, self)
def __iter__(self): return self
@JoshCheek
JoshCheek / how_to_implement_service_objects.rb
Last active Mar 12, 2021
A myriad of ways to implement "service objects"
View how_to_implement_service_objects.rb
# Note1: all `...` below mean "etcetera", like psuedocode, not implying the new Ruby 3 syntax
# Note2: `obj.()` is syntactic sugar for `obj.call()`
# Given an invocation like this:
OnboardOrganization.(arg1, arg2, ...)
# Here are some possible ways you could implement it:
# 1. If the definition fits in a s single method, you can toss it on a module
# You'd choose a module b/c you don't want a class here since you don't want to