Skip to content

Instantly share code, notes, and snippets.

JoshCheek /
Created Feb 22, 2021
Tailcall optimization in Ruby
# Remove the `-tco` from the end to run it without tailcall optimization
env RUBY_THREAD_VM_STACK_SIZE=100 ruby -e '<<~RUBY, tailcall_optimization: !!$tco).eval
def fibo(n, a=0, b=1)
return a if
fibo n-1, a+b, a
p fibo 500' -s -- -tco
JoshCheek / index.haml
Last active Feb 18, 2021
How to get Rails' radio buttons to work.
View index.haml
- @omghis.each do |omghi|
= image_tag "339x243") if
.omgsection { border: 1px solid black; }
= form_for @omghi, controller: :omghi, method: :create do |f|
JoshCheek / pg_recursive_query.rb
Last active Feb 16, 2021
PostgreSQL recursive querying to build a hierarchy
View pg_recursive_query.rb
require 'pg'
PG.connect(dbname: 'postgres').tap do |db|
db.exec "drop database if exists postgresql_experiments"
db.exec "create database postgresql_experiments"
PG.connect(dbname: 'postgresql_experiments').exec(<<~SQL).to_a
create table tree (id int, parent_id int);
JoshCheek /
Last active Feb 5, 2021
Refactoring Suggestions

Refactoring Suggestions

Hey, this morning I was thinking about the code we looked at yesterday, and how I'd refactor it. I came up with a few ideas, generally with overlapping reasons for the changes. I'm on a long train ride to St. Louis, and figured I'd write up what things I noticed and how I would make changes based on those things. It got a little long, b/c I had a few goals with it:

  • I wanted to make sure I explained my reasoning so that it wasn't just a religious
JoshCheek / example_of_hostile_sib_environment.rb
Last active Jan 24, 2021
Example of a hostile Seeing is Believing environment
View example_of_hostile_sib_environment.rb
# Users can fork the process, SiB still works
if !fork # => 6669
else # => 6668
# Users can exec, SiB still works
exec "echo users can exec, SiB still works" # printing is displayed at eof
JoshCheek / rubys_20_string_literals.rb
Last active Jan 23, 2021
Ruby's 20 string literals
View rubys_20_string_literals.rb
# Backticks and %x make a string literal and implicitly pass it to the backticks method,
# so if we define backticks to be identity, then that makes then equal to normal string literals.
# Though in practice, I've always taken advantage of the implicit invocation instead of nooping it.
def `(str)
# I'm ignoring different delimiters, on the percent literals.
# eg I consider %q(str) and %q[str] to be equivalent, because otherwise there'd
# probably be another 500 possibilities.
View classes_to_and_from_json_with_inheritance_and_such.rb
# A potential way to deal with
# include it to get reasonable JSONability
module JsonCreatable
def self.append_features(base)
base.extend ClassMethods
base.include InstanceMethods
module ClassMethods
JoshCheek /
Last active Jan 6, 2021
Sed is not a good language :(

Can you redeem sed's honour?

The premise

Been doing coding challenges. One of them is a golfing challenge, and you can use bash, I've got a really good Ruby solution, but figured I could get it even shorter if I used sed.

JoshCheek / clash_of_code.rb
Created Jan 5, 2021
Finally figured out this challenge from Clash of Code
View clash_of_code.rb
# It's a multiplexer, first bits select an output bit from the remaining bits.
# Only one person actually figured it out, and the others seem to think that person is a bot, so :shrug:"\n\n").map(&:lines).each do |test|
width, num_rows =
rows = test.take(num_rows)
expecteds = test.drop(num_rows).map(&:chomp)
outputs = do |row|
# first $width bits choose which of the remaining bits we select
JoshCheek / lambda-calculus-challenge.js
Last active Dec 18, 2020
Can Corey Haines figure out how to write the `block` function? (probablah nah)
View lambda-calculus-challenge.js
// The program in JavaScript: `for(let i=10; i; --i) console.log(i)`
// But THIS!! is the program in "lambda calculus JavaScript"!!!!
// Functoins that begin with a dollar sign cheat and use JS stuff
const $DIE = arg => { throw new Error("should not call!") }
// The rest of these cheat by assigning to constants instead of passing into variables,
// but none of them are self refferential, so that's just to make it easier to read