Skip to content

Instantly share code, notes, and snippets.


Josh Cheek JoshCheek

View GitHub Profile
JoshCheek / example.rb
Created Feb 25, 2021
Example of making an arg parser from a lambda signature
View example.rb
# Example works if done in args instead of local var assignment:
-> a, b=nil, c { {a: a, b: b, c: c} }[1, 2] # => {:a=>1, :b=>nil, :c=>2}
# Lib could, eg reflect on param types
def arg_parser(&block)
lambda do |argv|
argv, args = argv.dup, []
keywords = block.parameters.filter_map { |t, n| [n, false] if t == :keyreq }.to_h
while (arg = argv.shift)
next args << arg unless /^--?(.*)/ =~ arg
JoshCheek / subclassing_nil.rb
Last active Feb 23, 2021
Subclassing nil in Ruby 2.7.2 (the old version broke)
View subclassing_nil.rb
RUBY_VERSION # => "2.7.2"
require 'fiddle'
User = :name
class NullUser < NilClass
# We have no way to allocate a new NullUser... BUT! We can hack around this
# by making a new object and setting its class pointer to point at NullUser
instance =
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.