Skip to content

Instantly share code, notes, and snippets.

View kkuchta's full-sized avatar

Kevin Kuchta kkuchta

View GitHub Profile
@kkuchta
kkuchta / again.rb
Created March 19, 2023 06:47
A definition of a .again method in ruby
# A wrapper class that defines ".again", but passes through all other methods to
# the wrapped class.
class AgainWrapper < BasicObject
def initialize(wrapped_object, again_method)
@again_method = again_method
@wrapped_object = wrapped_object
end
def again
@wrapped_object.send(@again_method)
end
@kkuchta
kkuchta / blockformatting.rb
Created September 29, 2022 19:42
Block formatting example
require 'syntax_tree'
# Create a node visitor that walks the syntax tree looking for blocks.
class BlockFinder < SyntaxTree::Visitor
attr_reader :first_block
visit_method def visit_do_block(node)
puts "found a brace block node!"
@first_block ||= node
# Don't traverse further
end
`gem install -i /tmp/tmpgems --no-document pry-byebug`
Dir['/tmp/tmpgems/gems/*'].each { |gem| $LOAD_PATH << (gem + '/lib') }
require 'pry-byebug'
@kkuchta
kkuchta / list_comprehensions.rb
Created June 20, 2019 13:22
List comprehension in ruby
require 'pry-byebug'
module ListComprehension
def self.execute(block)
context = OuterBlockContextClass.new
# Set up variables + blocks
context.instance_eval(&block)
# evaluate the comprehension
@kkuchta
kkuchta / import.rb
Created June 5, 2019 14:15
A quick tool for importing a Quiver notes library to Drafts 5
require 'erb'
class Note
def initialize(qvnote)
@root_dir = qvnote
end
def parse
cell_string = cells.map do |cell|
case cell['type']
console = Object.new
def console.log(*x)
puts x.map(&:to_s).join(',')
end
class Object
def method_missing(*args)
return args[0]
end
end
@kkuchta
kkuchta / tiny.js
Created July 1, 2018 18:41
Tiny mandelbrot renderer. `npm install pngjs-image` then run with `node tiny.js`
const MIN = { x: -2, y: -1 }, MAX = { x: 1, y: 1 };
const WIDTH = HEIGHT = 1000;
function getEscapeCount(c) {
let z = { a: 0, b: 0 };
for (let i = 0; i < 100; i++) {
if (Math.pow(z.a, 2) + Math.pow(z.b, 2) > 4) return i;
z = { a: Math.pow(z.a, 2) - Math.pow(z.b, 2) + c.a, b: z.a * z.b + c.b };
}
}
ffmpeg -i audio.mp3 -f image2 -loop 1 -r 25 -i image.jpg -shortest -vcodec libx264 -acodec aac out.mp4
class Echoer
def call(env)
request = Rack::Request.new(env)
puts '---'
puts request.body.read
return [200, {}, ['ok']]
end
end
run Echoer.new
class Echoer
def call(env)
request = Rack::Request.new(env)
puts '---'
puts request.body.read
return [200, {}, ['ok']]
end
end
run Echoer.new