Skip to content

Instantly share code, notes, and snippets.

@tompng
tompng / rubykaigi2026logo.html
Created January 19, 2026 15:47
Render RubyKaigi 2026 logo to canvas
<canvas width="1600" height="1600" style="background:black"></canvas>
<script>
const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
const outer = [...new Array(30)].map((_, i) => {
const rot = 0.00
const th1 = Math.PI * 2 * (i - i % 6) / 30 + rot
const th2 = th1 + Math.PI * 2 / 5
const th = th1 + [0, 0.335, 0.34, 0.5, 0.66, 0.665][i % 6] * (th2 - th1)
const r = [1, 0.71, 0.62, 0.65, 0.62, 0.71][i % 6]
require 'prism'
require 'irb'
# Collect syntax-valid code snippets from the given directory
def each_snippet(dir, max_size)
files = Dir.glob("#{dir.delete_suffix('/')}/**/*.rb")
snippets = Set.new
handle_snippet = ->(code) {
return if snippets.include?(code)
snippets << code
@tompng
tompng / prism_parsey_fuzz.rb
Created November 26, 2025 17:38
Find parse inconsistency of Prism and parse.y
# Found inconsistencies
# a b do end.()
# begin a => a: end; a in a: if 1; case a; in a: end
# def a = a b do 1 end
# a rescue b = *c
# p(not a)
# x = a b and c
# def x = a b and c
# a(a 1 do end)
# a.(a 1 do end)
@tompng
tompng / log2_log10_bs.rb
Last active September 24, 2025 15:10
log2 log10 asymptotic binary splitting
# log(10): [[(1/8), 10], [(1/15), 13], [(1/24), 7]]
# log(2): [[(1/8), 3], [(1/15), 4], [(1/24), 2]]
def print_logn_params(base)
value_factors = {}
(8..30).each do |n|
num = (1+1r/n)
(0..30).each do |exp|
v = num ** exp
break if v > 2 * base
@tompng
tompng / faster_exp.rb
Last active September 15, 2025 12:33
def exp_bs(x, prec)
# Taylor series of exp can be converted to continued fraction as:
# exp(x)-1 = x*(1+x/2*(1+x/3*(1+x/4*(1+x/5*(1+...)))))
# Binary splitting can be applied to this continued fraction as:
# (1 + a0/a1 + x/a1 * (1 + b0/b1 + x/b1 * rest))
# (1 + ((a0+x)*b1+x*b0)/(a1*b1) + (x*x)/(a1*b1) * rest)
x = BigDecimal(x)
step = (1..).bsearch { |k| Math.lgamma(k)[0] / Math.log(10) - k * x.exponent > prec }
ms = (1..step).map { [0, BigDecimal(it)] }
while ms.size > 1
module YARD
class Parser; end
class TagHandler; end
class ParseResult; end
def self.process(comment, code_object)
result = Parser.parse(comment)
tag_handler = TagHandler.new(code_object)
result.tags.each do |name, arg|
tag_handler.handle(name, arg)
def sqrt(x, prec)
y = BigDecimal(Math.sqrt(x.to_f))
yinv = BigDecimal(1 / y.to_f)
(0..prec.bit_length).reverse_each do |i|
p = [[(prec >> i) + 2, BigDecimal.double_fig].max, prec].min
yinv = yinv.mult(BigDecimal(2).sub(y * yinv, p / 2), p / 2)
yinv = yinv.mult(BigDecimal(2).sub(y * yinv, p), p)
y = (y + x * yinv).div(2, p)
end
y
RackOnRactors::Minitra::Routes.draw do
before do
@foo = rand(1000)
end
get "/longlonglong" do
raise if Ractor.main?
"hello! " * @foo + request_header.inspect
end
end
@tompng
tompng / ntt_mult.rb
Last active August 17, 2025 04:37
NTT(Number Theorem Transform) multiplication
# Reference: https://qiita.com/AngrySadEight/items/0dfde26060daaf6a2fda
BASES = [29, 26, 24]
SHIFT = 27
class NTT
attr_reader :p
def initialize(base, shift)
@base = base
<body>
<script>
const canvas = document.createElement('canvas')
document.body.appendChild(canvas)
const size = canvas.width = canvas.height = 800
const ctx = canvas.getContext('2d')
const circles = [
{ x: 0.2, y: 0.6, r: 0.1 },
{ x: 0.7, y: 0.3, r: 0.07 },