Skip to content

Instantly share code, notes, and snippets.

@shugo
shugo / gist:0028538e723c3f46bdb219046ab5058b
Created December 3, 2025 01:21
strip vs gsub benchmark
voyager:tmp$ cat b.rb
require "benchmark"
TARGET = " \t\r\n\f\v\0" + "x" * 1024 + "\0 \t\r\n\f\v"
Benchmark.bmbm do |x|
x.report("strip") do
10000.times do
TARGET.strip
end
@shugo
shugo / gist:eb71108cae98798444e6311be4ef5976
Created November 16, 2025 10:58
media attachments statistics
mastodon@akagi:~/live$ psql -U mastodon -d mastodon_production -c "SELECT COUNT(*), COUNT(CASE WHEN file_storage_schema_version IS NULL OR file_storage_schema_version < 1 THEN 1 END) as old_schema, COUNT(CASE WHEN file_storage_schema_version >= 1 THEN 1 END) as new_schema, COUNT(CASE WHEN remote_url != '' THEN 1 END) as remote FROM media_attachments WHERE thumbnail_file_name IS NOT NULL;"
count | old_schema | new_schema | remote
-------+------------+------------+--------
13 | 0 | 13 | 13
(1 row)
@shugo
shugo / mastodon-url-share-with-hashtags.js
Last active November 19, 2025 03:24 — forked from hiko-p/mastodon-url-share-with-hashtags.js
閲覧中のWebページをmastodonに共有するためのブックマークレット。デフォルト付与するハッシュタグを決めておいたり、タイトルに含まれるハッシュタグなんかもカバーします。
javascript: (function () {
let title = document.title ? document.title : document.location.pathname.split('/').pop();
const url = document.URL;
const hashtagsearch = /\s*#\S+/g;
const hashsearch = /#/g;
const atsearch = /@/g;
/*
* 投稿先サーバFQDN
* 利用されているサーバに合わせて書き換えてください
@shugo
shugo / backup.sh
Last active November 12, 2025 03:10
Backup Mastodon DB and media
#!/bin/bash
set -euo pipefail
# Configurable variables
INSTANCE_DIR="/home/mastodon/live"
SYSTEM_DIR="/home/mastodon/live/public/system"
BACKUP_DIR="/home/mastodon/backup"
BACKUP_LIFETIME_DAYS=30
DATE_FORMAT="%Y%m%d"
DB_USER="mastodon"
; Zone: rubyist.net
$ORIGIN rubyist.net.
$TTL 3600
; NS Records
; MX Records
shibuya 300 IN MX 10 shibuya.rubyist.net.
; A Records
@shugo
shugo / strscan.rb
Created May 23, 2024 02:15
Pure Ruby StringScanner
class StringScanner
attr_reader :string
def initialize(string, *args)
@string = string
@pos = 0
@last_match = nil
end
def scan(re)
@shugo
shugo / example.rb
Created March 1, 2024 07:47
[Feature #20318] Example
# before
def balance(*args)
case args
in [RedFork[a, xk, xv, b], yk, yv, RedFork[c, zk, zv, d]]
RedFork[BlackFork[a, xk, xv, b], yk, yv, BlackFork[c, zk, zv, d]]
in [RedFork[RedFork[a, xk, xv, b], yk, yv, c], zk, zv, d]
RedFork[BlackFork[a, xk, xv, b], yk, yv, BlackFork[c, zk, zv, d]]
in [RedFork[a, xk, xv, RedFork[b, yk, yv, c]], zk, zv, d]
RedFork[BlackFork[a, xk, xv, b], yk, yv, BlackFork[c, zk, zv, d]]
in [a, xk, xv, RedFork[b, yk, yv, RedFork[c, zk, zv, d]]]
@shugo
shugo / nacl_quine.rb
Created November 8, 2023 11:10
NaCl Quine by yhara
eval$s=
%w(e="e val$s=% w("+$s+")*'' ";a=0;"
>KIZOaz(( *EHO1M< nz[MPb.~qirzBv0n'q T;lIup
oz6kZqF3{G pD+PSaA F1zJEw/,kBa>3f H_/D7+pAYH'O>NQ$g=JH6 E=C|Di
&~<2lBiHLV=r k+KjOcz )Bb~qVa1X)G2`[tb,& ".bytes {a=a*9 1+_1%9
2-1};$ ><<"\e [2J";d= (1..7). map{"\e [3#{_1
}m"};3 20.times{|k|h ="\e[1;1H";b=0;(0 ...10). each{|
v|h<<3 2.chr*(Math .sin(k /99.0) .abs*(10 -v));89 .time
s{h<<d [k*7/320, 3].samp le;h<<(a [v*89+_1]==1?(c=e[b] ;b+=1
;c):32) };h<<10} ;$><<h+"\e[ 0m";sl eep(0.01)};2001- 2023)*''
===============================================
PRK Firmware 0.9.20 (20230419 revision 21c7662)
PICORUBY_NO_MSC: false
prk-conf: ::
===============================================
STATS 92032/204800
Init Keyboard
Init GPIO
Configured as a split-type Anchor
@shugo
shugo / keymap.rb
Created April 19, 2023 11:02
Runner3680 keymap
kbd = Keyboard.new
kbd.split = true
kbd.init_pins(
[ 4, 5, 6, 7, 8 ],
[ 28, 27, 26, 22, 20, 23, 21 ]
)
kbd.add_layer :default, %i[