Skip to content

Instantly share code, notes, and snippets.

require 'ripper'
$VERBOSE = nil
tokens = <<'S'.split
def if true false nil + - = a A :: '' "#{
class module ? : . ( [ { -> | & * , / % ! @ $ ; < <<
S
loop do
code = (tokens.sample(9) + [' ']*5).shuffle.join
# code = tokens.sample(9).join(' ')
# RDOC_USE_PRISM_PARSER=1 bundle exec rdoc --op foo/bar
path_a = 'prism'
path_b = 'legacy'
files_a = Dir.chdir(path_a){Dir.glob('**/*.html')}
files_b = Dir.chdir(path_b){Dir.glob('**/*.html')}
def red(s) = "\e[31m#{s}\e[m"
def green(s) = "\e[32m#{s}\e[m"
@tompng
tompng / pow_k_sum.rb
Created July 6, 2024 09:53
sum of pow k
require 'matrix'
def powsum_params(k)
Matrix[*(k+2).times.map{|a|
(k+2).times.map{|b|(a+1)**b}
}].lup.solve((k+2).times.map{|n|(1..n+1).sum{_1**k}}).to_a
end
def assign_params(params, x)
params.each_with_index.sum {|p,i|p*x**i}
end
require 'rbs'
require 'rbs/cli'
loader = RBS::CLI::LibraryOptions.new.loader
def loader.each_dir
@loaded_dirs ||= Set.new
super do |source, dir|
next if @loaded_dirs.include? dir
@loaded_dirs << dir
yield source, dir
def prime_test?(n, a)
a.pow(n-1,n)==1
end
def ruby_mark(n,m,x,y)
0.5*x.abs+y.abs<n && y>-m
end
def pattern(ow, oh, iw, ih)
((1-oh)..ow).map{|y|
@tompng
tompng / git_changes.rb
Created June 15, 2024 15:53
git log additions and deletions
username = '...'
commits = `git log | grep -n1 "#{username}"`.scan(/-commit ([0-9a-f]+)/).flatten
result = commits.map do |commit|
stat = `git show #{commit} --stat -- lib`
diff = stat[/(\d+) insertion/].to_i - stat[/(\d+) deletion/].to_i
[commit, diff]
end
add_deletes = commits.map do |commit|
stat = `git show #{commit} -- lib`
@tompng
tompng / map_edit.rb
Created May 28, 2024 16:13
input: white-black map, output: rk2024-color map
require 'chunky_png'
img = ChunkyPNG::Image.from_file('okinawa_ishigaki_small.png')
alphas = img.height.times.map do |y|
img.width.times.map do |x|
((img[x,y] >> 8) & 0xff) / 255.0
end
end
lines = Hash.new(0)
borders = Hash.new(0)
files_rbreadline = Dir.glob('rb-readline/lib/**/*.rb')
files_reline = Dir.glob('reline/lib/**/*.rb')
K = 123456789
P = (1 << 81) | 17
def hashes(text, n)
output = {}
hash = 0
hash2 = 0
@tompng
tompng / irbrc
Last active June 7, 2024 05:04
return if ENV['WITHOUT_IRBRC']
{ IRB: 'irb', Reline: 'reline', ReplTypeCompletor: 'repl_type_completor' }.each do |const_name, lib|
require lib
const = Object.const_get(const_name)
version = const::VERSION
dir = File.expand_path File.join(File.dirname(const.const_source_location(:VERSION).first), '../../')
if File.exist?(File.join(dir, '.git'))
Dir.chdir dir do
branch = `git branch`[/^\* .+/][2..]
%w@state=32;(st@=>s;at_exit{eval(
c=s*'')};s+=%w@ate..).each{|s|a=64.ch
r;t=((-11..11).map{|iy|((-22..22).map{|ix
|x,y=((ix+iy*2i)*(1i**(s/16r))).rect;[x.abs+(
y-5).abs,y+10].max<23.4??X:32.chr}*'').rstrip