Skip to content

Instantly share code, notes, and snippets.

@knugie
knugie / vector_cluster.rb
Last active June 29, 2016 19:05
Cluster vectors by their similarity defined by a tolerance vector
require 'matrix'
tolerance = Vector[5, 5, 5]
elements = [Vector[2, 1, 4], Vector[20, 100, 25], Vector[21, 98, 21], Vector[1, 2, 3]]
clusters = []
while(elements.any?)
element = elements.pop
cluster, elements = elements.partition do |elem|
diff = tolerance - (elem - element).map(&:abs)
#!/usr/bin/env ruby
###################################################
## Display screen dimensions in macOS using ruby ##
##################################################
# found https://developer.apple.com/reference/coregraphics/1456395-cgdisplaybounds?language=objc
# --> use CoreGraphics framework
# --> CGRect CGDisplayBounds(CGDirectDisplayID display);
#############################################
@knugie
knugie / mov2mp4.sh
Last active October 6, 2022 14:22
convert .mov to .mp4 using ffmpeg
ffmpeg -i movie.mov -vcodec copy -acodec copy out.mp4
@knugie
knugie / mp42gif.sh
Last active May 25, 2022 19:35
Generate a gif from an mp4 file
# Using ffmpeg
ffmpeg -y -i video.mp4 -vf fps=1,scale=600:-1:flags=lanczos,palettegen palette.png
ffmpeg -i video.mp4 -i palette.png -filter_complex "setpts=0.125*PTS,fps=5,scale=600:-1:flags=lanczos[x];[x][1:v]paletteuse" video.gif
# OR
# Using ffmpeg and gifsicle (https://www.lcdf.org/gifsicle/)
ffmpeg -i video.mp4 -s 600x400 -r 3 -f gif - | gifsicle --delay=3 > video.gif
@knugie
knugie / keybase.md
Last active May 25, 2022 19:46
Keybase proof

Keybase proof

I hereby claim:

  • I am knugie on github.
  • I am knugie (https://keybase.io/knugie) on keybase.
  • I have a public key whose fingerprint is 4F54 2255 6028 FAFB 7458 E545 4276 8876 DFB2 3E2B

To claim this, I am signing this object:

#!/usr/bin/env bash
size=1024 # MB
mount_point=$HOME/tmp
name=$(basename "$mount_point")
usage() {
echo "usage: $(basename "$0") [mount | umount | remount | check | orphan]" \
"(default: mount)" >&2
}
@knugie
knugie / worker_pool.rb
Last active May 25, 2022 19:34
Worker pool in Ruby
# from https://hspazio.github.io/2017/worker-pool/
SIZED_QUEUE_SIZE = 10
############################################################################
def fib(n)
n < 2 ? n : fib(n-1) + fib(n-2)
end
############################################################################
@knugie
knugie / solution.rb
Created September 2, 2018 20:47
How many 3 digit numbers have exactly 3 factors?
[*100..999].count{ |x| (1..x).count { |i| x % i == 0 } == 3 }
# => 7
@knugie
knugie / example.sh
Last active August 11, 2019 19:18
ASCII art with figlet
# brew install figlet
# showfigfonts
# example:
figlet -D -n -f fender "The quick brown fox jumps over the lazy dog [ \ ] { | } ~"
# |''||''| '|| '||
# || || '' ||
# || ||''|, .|''|, .|''||` '|| ||` || .|'', || //`
# || || || ||..|| || || || || || || ||<<
# .||. .|| || `|... `|..|| `|..'|. .||. `|..' .|| \\.
@knugie
knugie / example_encrypt_decrypt.rb
Created May 28, 2020 19:32
Encrypt, Decrypt large files
# From https://tjay.dev/howto-working-efficiently-with-large-files-in-ruby/
# Encrypt
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.encrypt
cipher.key = KEY
cipher.iv = IV
file = nil
enc_file = nil