Skip to content

Instantly share code, notes, and snippets.

Josh Cheek JoshCheek

View GitHub Profile
@JoshCheek
JoshCheek / json_parse.rb
Created Apr 22, 2020
Not sure why, but I suddenly felt compelled to make a JSON parser 🤷‍♂️
View json_parse.rb
def json_parse(json)
success, index, _ws = json_parse_optional_whitespace(0, json)
success, index, value = json_parse_value(index, json)
raise "Could not parse" unless success
value
end
def json_parse_value(index, json)
%I[
json_parse_null
@JoshCheek
JoshCheek / refining_enumerable_reduce.rb
Last active Feb 27, 2020
Using refinements to monkey patch a reversed `reduce` in Ruby
View refining_enumerable_reduce.rb
module Consistency
original = Enumerable.instance_method :reduce
refine Enumerable do
# I didn't actually check it against the real impl or docs or anything, but its something like this
alias_method :inject, define_method(:reduce) { |*args, &block|
block = args.pop if args.size == 2
Enumerator.new do |y|
original.bind(self).call(*args) { |l, r| y.yield r, l }
end.each &block
}
View rot_13.rb
def rot13(ascii_char)
ascii_offset = 65
alphabet_size = 26
full_index = ascii_char.ord - ascii_offset
cap_bit = full_index & 0b100000
half_index = full_index & 0b011111
rotated = (half_index + alphabet_size/2) % alphabet_size
(rotated + cap_bit + ascii_offset).chr
end
View timing_grape_api.rb
require 'grape'
require 'logger'
require 'json'
class MyApi < Grape::API
version 'v1', :using => :path
format :json
desc "Returns first / last name"
get '/:first/:last' do
@JoshCheek
JoshCheek / trunk_based_development
Last active Dec 3, 2019
Trunk Based Development
View trunk_based_development
Trunk Based Development
=======================
Disclaimer:
I've never done this on a deployed flagship app at a job.
Words:
Continuous integration: all code is merged frequently (perhaps several times per day to once every few days)
Continuous delivery: the integrated code is delivered (tested and deployable)
Continuous deployment: the deliverable code is deployed (shipped)
Release: feature is available to users
@JoshCheek
JoshCheek / tetris
Last active May 25, 2020
CLI tetris
View tetris
#!/usr/bin/env ruby
require 'io/console'
class Grid
def self.from_str(str, color:)
rows = str.lines.map do |line|
line.chomp.chars.map do |c|
color if c != " ".freeze
end
end
@JoshCheek
JoshCheek / lonpos.rb
Last active Dec 1, 2019
Lonpos Pyramid
View lonpos.rb
class Piece
def self.from_strs(*strs, **opts)
new strs.map { |s| s.chars.map { |c| c == "*" } }, **opts
end
attr_reader :rows, :graphic
def initialize(rows, graphic:)
@graphic, @rows = graphic, rows.map do |row|
row.map { |cell| cell == true ? graphic : (cell || nil) }
@JoshCheek
JoshCheek / dot_is_for_minitest.rb
Created Sep 25, 2019
Experiment that adds `.is` (like the old RSpec `.should`) to Minitest
View dot_is_for_minitest.rb
require 'minitest/autorun'
module DotIs
# Track the current test so that we can run assertions in its context
def self.current_test=(test)
Thread.current[:dot_is_current_test] = test
end
def self.current_test
Thread.current.fetch :dot_is_current_test
@JoshCheek
JoshCheek / metaprogrammed_active_record_clone.rb
Created Sep 24, 2019
Metaprogrammed ActiveRecord "clone" (just a fun morning experiment)
View metaprogrammed_active_record_clone.rb
class Query
def initialize(table, db)
@db, @table, @cols, @rest = db, table.downcase, '*', ''
end
def method_missing(clause, *args)
clause == :select \
? @cols = args.join(', ') \
: @rest << ' ' << [clause, *args].join(' ')
self
end
@JoshCheek
JoshCheek / lambda-calc.js
Created Sep 23, 2019
lambda calc translation
View lambda-calc.js
($true => $false => $if => y => succ => zero => toJs =>
(one => two => three => add => nil => cons =>
(multiply => map => reduce =>
console.log(reduce(arr => n => [...arr, toJs(n)])(
// Translation of this program into lambda calculus
// https://dev.to/laurieontech/human-readable-javascript-337o
//
// const arr = [1,2,3]
// const timesTwoPlusIndex = (el, index) => (el*2) + index
You can’t perform that action at this time.