Skip to content

Instantly share code, notes, and snippets.

# solution to https://gist.github.com/JoshCheek/2641441
class MyStruct
def self.new(*members, &block)
members.none? and raise ArgumentError, 'wrong number of arguments (must provide at least one)'
members.each { raise TypeError, "Expected a Symbol, got #{_1.inspect}" unless _1.is_a? Symbol }
Class.new Instance do
@members = members
members.each do |name|
define_method(name) { self[name] }
@JoshCheek
JoshCheek / parsing_pretty_inspect.sh
Created April 29, 2021 07:03
Script I ran to parse a `pretty_inspect` output that was 84k lines long and show me the path to the lines of interest
ruby < omg -r coderay -lne '
BEGIN {
target = /DefaultTrailApplicability/ # needle
stack = [] # haystack
results = []
indentation = -> line { line.to_s[/\A\s*/].size }
String.class_eval { attr_accessor :lineno }
}
$_.lineno = $. # each line tracks its line number
@JoshCheek
JoshCheek / Gemfile
Last active April 16, 2021 19:05
Testing GH JSON / JSON5 / YAML highlighting
source 'https://rubygems.org'
gem 'json5'
@JoshCheek
JoshCheek / pry_wherever_exceptions_get_raised.rb
Last active April 15, 2021 18:44
Reproducing pry-exception_explorer
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 April 14, 2021 01:46
Writable views to make denormalization less annoying
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
@JoshCheek
JoshCheek / TrixEditor.js
Last active April 5, 2021 19:55
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 April 2, 2021 11:26
Refactoring + code review of some code I saw in a blog
@JoshCheek
JoshCheek / syncing_promises.js
Created April 1, 2021 23:39
JS syncing promises code sample
# 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 April 1, 2021 17:42
Example of how you could use Ruby's WeakRef to perform weak memoization
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 March 24, 2021 18:09
Show unused / untested stuff in Rails
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 = {