Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / most_allocated_objects.rb
Last active October 16, 2022 09:31
Finding locations of most allocated objects
require 'objspace'
def self.show_allocations(&block)
_ = ObjectSpace.trace_object_allocations &block
ObjectSpace
.each_object
.to_a
.filter_map do |obj|
file = ObjectSpace.allocation_sourcefile obj
line = ObjectSpace.allocation_sourceline obj
@JoshCheek
JoshCheek / finding_where_objs_come_from.rb
Created October 6, 2022 13:13
Figuring out where objects are coming from
# followup for https://twitter.com/kirill_shevch/status/1577930057728856064
require 'objspace'
require 'json'
def some_code
[ 10_000.times.map { "hi" },
5_000.times.map { "there" },
JSON.load('{ "omg": 123, "wtf": true, "bbq": 23.45 }'),
]
end
@JoshCheek
JoshCheek / active_record_query_ordering.rb
Created October 4, 2022 10:32
Experiment on ordering an active record query
require 'active_record'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = false
create_table :posts do |t|
t.string :title
t.string :status
end
@kylechui
kylechui / dot-repeating.md
Last active April 28, 2025 18:47
A basic overview of how to manage dot-repeating in your Neovim plugin, as well as manipulate it to "force" what action is repeated.

Adding dot-repeat to your Neovim plugin

In Neovim, the . character repeats "the most recent action"; however, this is not always respected by plugin actions. Here we will explore how to build dot-repeat support directly into your plugin, bypassing the requirement of dependencies like repeat.vim.

The Basics

When some buffer-modifying action is performed, Neovim implicitly remembers the operator (e.g. d), motion (e.g. iw), and some other miscellaneous information. When the dot-repeat command is called, Neovim repeats that operator-motion combination. For example, if we type ci"text<Esc>, then we replace the inner contents of some double quotes with text, i.e. "hello world""text". Dot-repeating from here will do the same, i.e. "more samples""text".

Using operatorfunc

@lkhphuc
lkhphuc / nvim_highlight_update.md
Last active December 25, 2023 06:20
A simple utility function to override and update any colorscheme in Neovim.

I like to have my neovim to bold the language keywords, italicize the comments, or both italicize and bold all the builtin functions, type or constants etc. And I want this to be consistent across colorschemes.

In vim/neovim, to update a highlight group it is as simple as highlight HighlightGroupToModified gui=bold,italic,underline. But this is problematic if the HighlightGroupToModified is defined as a link to other highlight group. For example highlight TSInclude might be linked to Include by your colorscheme, so executing hi TSInclude gui=bold will apply bold to TSInclude but wipe out any foreground and background color information that was originally defined by linking to Include highlight group.

Since every colorschemes can arbitrarily choose to define a highlight group explicitly or by linking, adding hi TSConstant gui=bold to your config are bound to break when you switch colorschemes. A manual approach is to recursively go down the linking chain defined by colorscheme, e.g TSConstant

@codeinthehole
codeinthehole / 1pw-item-users
Created June 29, 2022 21:29
Bash script for listing the users who have access to a 1Password item
#!/usr/bin/env bash
#
# Print the users who have access to a given 1Password item.
#
# Usage:
#
# 1pw-item-users "$ITEM_NAME"
#
# Note, the `op` tool must be authenticated before this command is run.
@kddnewton
kddnewton / json.rb
Last active December 18, 2024 17:34
JSON parser with pattern matching
require "json"
struct = { "a" => 1, "b" => 2, "c" => [1, 2, 3], "d" => [{ "e" => 3 }, nil, false, true, [], {}] }
source = JSON.dump(struct)
tokens = []
index = 0
until source.empty?
tokens <<
@davidatsurge
davidatsurge / luasnip_config.lua
Last active September 24, 2024 23:26
luasnip+treesitter snippet that fills in prop names when creating new react component
local ls = require("luasnip")
local fmt = require("luasnip.extras.fmt").fmt
local s = ls.snippet
local i = ls.insert_node
local f = ls.function_node
local d = ls.dynamic_node
local sn = ls.snippet_node
local rep = require("luasnip.extras").rep
-- Get a list of the property names given an `interface_declaration`
@JoelQ
JoelQ / dollar.rb
Last active September 23, 2024 03:02
Implementing value object semantics as an RSpec shared_example.
class Dollar
attr_reader :cents
def initialize(cents:)
@cents = cents
end
def hash
[self.class, cents].hash
end
@joelonsql
joelonsql / PostgreSQL-EXTENSIONs.md
Last active April 28, 2025 19:47
1000+ PostgreSQL EXTENSIONs

🗺🐘 1000+ PostgreSQL EXTENSIONs

This is a list of URLs to PostgreSQL EXTENSION repos, listed in alphabetical order of parent repo, with active forks listed under each parent.

⭐️ >= 10 stars
⭐️⭐️ >= 100 stars
⭐️⭐️⭐️ >= 1000 stars
Numbers of stars might not be up-to-date.