Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / 1-fizz_buzz.rb
Created December 11, 2011 18:47
Ruby Code Golf solutions
puts Solution.new('1. Fizz Buzz', <<SOLUTION)
def fizzbuzz(n)(n%15==0?'FizzBuzz':n%3==0?'Fizz':n%5==0?'Buzz':n).to_s end
SOLUTION
.test { fizzbuzz 3 }.expect { |result| result == "Fizz" }
.test { fizzbuzz 10 }.expect { |result| result == "Buzz" }
.test { fizzbuzz 45 }.expect { |result| result == "FizzBuzz" }
.test { fizzbuzz 31 }.expect { |result| result == "31" }
@JoshCheek
JoshCheek / touch.c
Created September 18, 2011 05:51
source code for Gnu's touch command
/* touch -- change modification and access times of files
Copyright (C) 1987, 1989-1991, 1995-2005, 2007-2011 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
@JoshCheek
JoshCheek / example_of_postgresql_ltree.rb
Last active January 23, 2024 13:12
Example of PostgreSQL's ltree
require 'pg'
db = PG.connect dbname: 'josh_testing'
db.exec 'begin'
def db.exec(*)
super.to_a
rescue
$!.set_backtrace caller.drop(1)
raise
end
@JoshCheek
JoshCheek / run.sh
Created December 17, 2023 08:32
CT36 banners and relics
curl -s 'https://sciplypandora.github.io/static/json/configs/CT36.json' |
ruby -r json -e '
puts JSON.parse($stdin.read, symbolize_names: true)
.[](:tiles)
.reject { |code, tile| tile[:tile_type] == "regular" }
.map { |code, tile|
tile_type = tile[:tile_type]
game_type = tile[:game_type].sub("least_cash", "lc").sub("least_tiers", "lt")
boss = tile[:boss] || " "
relic = tile[:relic]
@JoshCheek
JoshCheek / uninstall_snap_camera_mac_osx.sh
Last active October 31, 2023 23:36
How to uninstall Snap Camera on Mac OS X
# these are reconstructed from a shell session without runnig them, so make
# sure you check that it's a sane thing to do before running it, I make no
# guarantees of fitness, and accept no liability. Run at your own risk.
sudo launchctl remove com.snap.SnapCameraRemover
rm -r ~/Library/Caches/Snap/
rm -r ~/Library/Caches/com.snap.SnapCamera/
rm -r ~/Library/Preferences/Snap/
rm ~/Library/Preferences/com.snap.SnapCamera.plist
rm ~/Library/Preferences/com.snap.Snap\ Camera.plist
sudo rm -rf /Applications/Snap\ Camera.app/
@JoshCheek
JoshCheek / parse_otf.rb
Last active August 10, 2023 11:44
Parsing WOFF / OTF fonts in Ruby
# WOFF spec https://www.w3.org/TR/2012/REC-WOFF-20121213/
# OTF spec https://www.microsoft.com/en-us/Typography/OpenTypeSpecification.aspx
# Really nice inspector https://opentype.js.org/font-inspector.html
# unpack instructions http://www.rubydoc.info/stdlib/core/String#unpack-instance_method
# Font programming instruction definitions https://developer.apple.com/fonts/TrueType-Reference-Manual/RM05/Chap5.html#WS
# Font forge has some useful info, too, get it with homebrew cask
FONT_FILE = '/Users/xjxc322/gamut/bots/pxgamut_regular.woff'
require 'zlib'
# Values found with `stty -g`
# Turns out you can do `stty -a` to get human readable values
# Context is at https://github.com/pry/pry/issues/1290#issuecomment-55916557
configs = [
[:broken_pry, "gfmt1:cflag=4b00:iflag=2b02:lflag=200005cf:oflag=3:discard=f:dsusp=19:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=16:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=17:ispeed=38400:ospeed=38400"], # => [:broken_pry, "gfmt1:cflag=4b00:iflag=2b02:lflag=200005cf:oflag=3:discard=f:dsusp=19:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=16:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=17:ispeed=38400:ospeed=38400"]
[:good_term, "gfmt1:cflag=4b00:iflag=6902:lflag=200005cf:oflag=3:discard=f:dsusp=19:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=16:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=17:ispeed=38400:ospeed=38400"], # => [:good_term, "gfmt1:cflag=4b00:iflag=6902:lflag=200005cf:oflag=3:discard=f:dsusp=19:eof=4:eol
@JoshCheek
JoshCheek / void_value_expressions.rb
Last active April 11, 2023 14:11
void value expressions make no sense
-> { a = case when true then return end } # this is allowed
-> { a = if true then return end } # this is not
-> { a = if true then return; 2 end } # this is
-> { a = (true && return) } # this is allowed
-> { a = (return && true) } # this is not
-> { a = begin; return
rescue; return
ensure; return
@JoshCheek
JoshCheek / authenticated_field.go
Last active March 16, 2023 23:03
Example of how we can prob use resolvers in graphql-go so that you only need to be authenticated if you are asking for fields that require it.
package main
import (
"context"
"encoding/json"
"fmt"
gql "github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/directives"
)
@JoshCheek
JoshCheek / README.md
Last active March 7, 2023 10:06
A program that is both a palindrome and a quine, made with hearts for Joe

A program that is a palindrome (if you reverse its source code, you get the same program) and is a quine (if you run it, it prints out its own source code). Verify like this:

ruby palindrome_quine.rb | tee output
diff palindrome_quine.rb output # the output is the same as the source program
ruby -e 'puts $stdin.read.reverse' < palindrome_quine.rb > reversed
diff palindrome_quine.rb reversed # the reversed program is the same as the source program