Skip to content

Instantly share code, notes, and snippets.

def dump(s)
dumped = s == eval("'#{s}'") && s !~ /[\C-a-\C-z]/ ? "'#{s}'" : s.dump.gsub(/\\x\d\d/){
"\\C-"+('a'.ord + _1[2..].to_i(16) - 1).chr
}
if dumped =~ /\u3099/
dumped.gsub!("\u3099", "\\u3099")
dumped = ?" + dumped[1...-1] + ?" if dumped[0] == ?'
end
dumped
end
@tompng
tompng / regexp_test.rb
Created December 27, 2023 12:05
regexp
class Generator
def generate_paren(level)
regexp, producer = multi(level - 1)
[['(', '(?:', '(?<a>'].sample + regexp + ')', producer]
end
ALPHABET = ('a'..'z').to_a
def generate_alphabet(_level)
c = ALPHABET.sample
require 'matrix'
module RegexpMatrixSolver
refine Array do
def ~
keys = map(&:first).flat_map(&:keys).uniq
matrix = []
binding = first.last
vec = map{_2}
each do |values, rhs|
matrix << keys.map { values[_1] || 0 }
def run
$VERBOSE=nil
tokens = %w[1 + * ** a in ( ) [ ] = . .. => <<]
cnt = 0
output = {}
(2..8).each do |n|
cnt = 0
tokens.repeated_permutation(n).each do |tokens|
code = tokens.join('') # or join(' ')
# Ruby's bug already fixed in 3.3
module RequirePaths
class << self
def require_completions(path)
*dir, target = path.split(File::SEPARATOR, -1)
target ||= ''
with_cache [:require_completions, dir] do
gem_and_system_load_path.flat_map do |load_path|
base_dir = File.absolute_path(File.join(load_path, *dir))
with_cache [:requireable_paths, base_dir] do
requireable_paths(base_dir).select { |p| p.start_with?(target) }
,,,;;;;''''';;;'';,
,,;'' ';;,;;; ',
,,'' ;;'';'''';;;;;;
,;' ;; ',, ;
,;' ,;' ';, ;
;' ,;; ',,,;
,' ,;;,,,,,,,,,,,;;;;
;' ;;';;;; ,;;
;' ,;' ;; '',, ,;;;
;; ,;' ; '';, ,; ;'
@tompng
tompng / status.rb
Created October 17, 2023 10:39
ステータス下に出すやつ
require 'io/console'
num_status_lines = 4
$SYMBOLS = Symbol.all_symbols
def status
bold = "\e[1m"
red = "\e[31m"
green = "\e[32m"
reset = "\e[m"
[
Dir.chdir File.dirname __FILE__
katakata_dir = '../../../../../tompng/katakata_irb'
def replace_module_names(code)
code
.gsub('module KatakataIrb', 'module IRB::TypeCompletion')
.gsub('KatakataIrb::TypeAnalyzer', 'TypeAnalyzer')
.gsub('KatakataIrb::Types', 'Types')
.gsub('KatakataIrb::Scope', 'Scope')
@tompng
tompng / termination_continue_checker.rb
Created October 14, 2023 14:45
script to help refactor termination check logic of irb
require 'set'
require 'ripper'
end_of_input_pattern = /syntax error, unexpected end-of-input/
end_of_file_pattern = /unterminated (?:string|regexp) meets end of file/
errors = Set.new
$VERBOSE = nil
handle = ->(e, file, code) {
m = e.message.lines.first
next if m.match?(end_of_input_pattern) || m.match?(end_of_file_pattern) || m.match?(/anywhere before EOF/)
next if errors.include?(m)
@tompng
tompng / snow.rb
Last active October 3, 2023 06:42
snow quine
            *          *                 
    *             *   *      *            
        *            *                   
         *          *      *              
          *        *                  *   
                            *      *     
       *        *     *                  
   *                                *   
        *          *           *        
          *                   *      *