Skip to content

Instantly share code, notes, and snippets.

Avatar

Josh Cheek JoshCheek

View GitHub Profile
@JoshCheek
JoshCheek / example.rb
Created Feb 25, 2021
Example of making an arg parser from a lambda signature https://twitter.com/josh_cheek/status/1364729672492277762
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
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 = Struct.new :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
def self.new(*)
instance = Object.new
@JoshCheek
JoshCheek / tco_example.sh
Created Feb 22, 2021
Tailcall optimization in Ruby
View tco_example.sh
# Remove the `-tco` from the end to run it without tailcall optimization
env RUBY_THREAD_VM_STACK_SIZE=100 ruby -e '
RubyVM::InstructionSequence.new(<<~RUBY, tailcall_optimization: !!$tco).eval
def fibo(n, a=0, b=1)
return a if n.zero?
fibo n-1, a+b, a
end
RUBY
p fibo 500' -s -- -tco
@JoshCheek
JoshCheek / index.haml
Last active Feb 18, 2021
How to get Rails' radio buttons to work.
View index.haml
%ul
- @omghis.each do |omghi|
%li
= omghi.wtf
= image_tag omghi.media.variant(resize: "339x243") if omghi.media.present?
:css
.omgsection { border: 1px solid black; }
= form_for @omghi, controller: :omghi, method: :create do |f|
@JoshCheek
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"
end
PG.connect(dbname: 'postgresql_experiments').exec(<<~SQL).to_a
create table tree (id int, parent_id int);
@JoshCheek
JoshCheek / refactoring_suggestions.md
Last active Feb 5, 2021
Refactoring Suggestions
View refactoring_suggestions.md

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
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
Process.pid # => 6669
else
Process.pid # => 6668
# Users can exec, SiB still works
exec "echo users can exec, SiB still works" # printing is displayed at eof
end
@JoshCheek
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)
str
end
# 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 https://twitter.com/postmodern_mod3/status/1348330707420991497
# include it to get reasonable JSONability
module JsonCreatable
def self.append_features(base)
base.extend ClassMethods
base.include InstanceMethods
end
module ClassMethods
@JoshCheek
JoshCheek / README.md
Last active Jan 6, 2021
Sed is not a good language :(
View README.md

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.