Skip to content

Instantly share code, notes, and snippets.

mut = Ractor.new do
pipe = Ractor.new do
loop { Ractor.yield Ractor.receive }
end
loop do
Ractor.yield pipe
pipe.take
end
end
require 'prism'
require 'ripper'
def parsey_extract_locations(code)
lines = code.lines
line_indexes = lines.reduce([0]) { _1 << _1.last + _2.bytesize }
code_index = ->(line, col) { line_indexes[line - 1] + col }
locations = []
each_node = -> node do
locs = node.locations.compact
all_tokens = %w[
x
y
false
if
rescue
begin
end
=
def
require 'irb'
require 'stringio'
def count_lines(ascii_text, width)
ascii_text.lines.sum{|l| [(l.chomp.size-1)/40+1,1].max }
end
10000.times do |i|
width = 40
height= 20
out = IRB::Pager::PageOverflowIO.new(width, height, ->*{})
require 'prism'
Dir.glob('../**/*.rb').each do |file|
next if file =~ /(^|\/)tmp\//
codes = []
prc = ->(node) {
codes << node.slice
codes << node.unescaped if node.is_a? Prism::StringNode
node.compact_child_nodes.each { prc.call it }
}
@tompng
tompng / dir_upload_client.rb
Last active December 14, 2024 05:29
simple directory uploader
require 'socket'
require 'json'
endpoint = ARGV[0]
src = ARGV[1]
dst = ARGV[2] || ''
unless endpoint =~ /\A.+:\d+\z/ && src
puts "ARGV[0]: host:port"
puts "ARGV[1]: src dir"
puts "ARGV[2]: dst dir"
puts <<RUBY.b
eval eval eval eval eval <<'CODE'
$>.set_encoding 'utf-8'
$><<"#{'あ'.b}"
<<'A'
# encoding: euc-jp
$><<"#{'い'.encode('euc-jp').b}"
<<'B'
# encoding: sjis
$><<"#{'う'.encode('sjis').b}"
@tompng
tompng / wood_pattern.rb
Created November 19, 2024 15:29
雑な木目パターン
require 'js'
puts 'hello'
document = JS.global['document']
canvas = document.createElement('canvas')
canvas['width'] = 256
canvas['height'] = 256
document['body'].appendChild(canvas)
$context = canvas.getContext('2d')
def set_color(x, y, rgb)
@tompng
tompng / complex_pow.rb
Last active November 9, 2024 05:48
complex pow in 1024bit
def complex_fixed_point_1024bits_pow(x, n, bits: 1024)
ans = base = 1 << bits
x2 = Complex.rect(*x.rect.map{(_1.to_r * base).round})
mult = ->(a, b) { Complex.rect(*(a * b).rect.map { _1 / base }) }
n.digits(2).each do |a|
ans = mult[ans, x2] if a == 1
x2 = mult[x2, x2]
end
ans.fdiv base
end
@tompng
tompng / term_value.md
Last active October 18, 2024 17:54
List of Environment variable TERM

TERM value that has different kcuu1|kcud1|kcuf1|kcub1|khome|kend|kpp|knp|kdch1 value from the one Reline registers when terminfo is not available

TERM count values
aaa-60 8 \e[A \e[B \e[C \e[D \e[H - - - \e[P
aixterm 2 \e[A \e[B \e[C \e[D \e[H \e[146q \e[150q \e[154q \e[P
cons25 17 \e[A \e[B \e[C \e[D \e[H \e[F \e[I \e[G \x7F
d210-dg 2 \x17 \x1A \x18 \x19 \b - - -
mach 19 \e[A \e[B \e[C \e[D \e[H \e[Y \e[V \e[U \e[9
qansi 35 \e[A \e[B \e[C \e[D \e[H \e[Y \e[V \e[U \e[P