Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / api_only_rails_example.rb
Last active July 17, 2024 08:23
How to configure API only Rails (esp how to deal with param parsing).
# I submitted a bug report https://github.com/rails/rails/issues/34244
# b/c Rails was not honouring my `rescue_from` block which was causing my API to
# be inconsistent. I was told this is expected behaviour. I think it's probably
# fine for an HTML app where you control the form inputs. But for an API app,
# the API is public facing and very important, so Rails shouldn't have its own
# special errors that bypass my app's configuration and make my API inconsistent.
#
# Decided it shouldn't be too difficult to handle this myself. So, here is my
# solution. It contains most of the important lessons I've learned about how to
# get a Rails API app setup. It removes Rails' params parsing and adds its own.
@JoshCheek
JoshCheek / ruby_2_and_3_keyword_args.rb
Created July 31, 2021 03:48
Ruby 2 & 3's keyword args and how `ruby2_keywords` works
# Helpers to make the code below easier to read
class Array
def keywords?() = Hash.ruby2_keywords_hash?(self[-1])
def to_keywords!() = (self[-1] = Hash.ruby2_keywords_hash(self[-1]))
end
# Code to facilitate the examples below
def record_args(*args) = args
def call_this(val:) = val
@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