Skip to content

Instantly share code, notes, and snippets.

Josh Cheek JoshCheek

View GitHub Profile
@JoshCheek
JoshCheek / jit.md
Created Mar 22, 2019
Ruby with --jit
View jit.md
View async_promises.js
// felt like it should come with an example: https://twitter.com/taniarascia/status/1108358621908340736
const promises = [1000, 2000, 3000].map(n => new Promise(resolve => setTimeout(resolve, n, n))) // 1
const promisesPromise = Promise.resolve(promises) // 2
// you cant do async at the toplevel, so we need to put it in an iife
void async function() {
console.log("===== map =====")
// to prevent it from moving on, while the promises are being waited for,
// we need to map the input promises to the output promises and then wait for all of them
// it's pretty painful
@JoshCheek
JoshCheek / undocumented_core.rb
Last active Mar 19, 2019
Undocumented core Ruby
View undocumented_core.rb
# code mostly taken from here: https://github.com/pry/pry-doc/blob/9eb2d6f5408256a88d3a97f22d596ac6db14e7db/Rakefile
# (modified a little)
#
# NOTE: You'll need:
# * wget (I did `brew install wget`)
# * latest_ruby (I did `gem install latest_ruby`)
# * yard (I did `gem install yard`)
require 'latest_ruby'
def generate_docs_for(ruby_ver, latest_ruby)
@JoshCheek
JoshCheek / include_and_extend_in_ruby.rb
Last active Mar 20, 2019
Include and Extend in Ruby
View include_and_extend_in_ruby.rb
# trying to be helpful for https://twitter.com/coorasse/status/1108047414961811456
require 'objspace'
# Helper methods
public
def real_class() ObjectSpace.internal_class_of self end
def real_super() ObjectSpace.internal_super_of self end
def references() ObjectSpace.reachable_objects_from self end
# A module we'll include
@JoshCheek
JoshCheek / iterative_ruby_interpreter.rb
Created Mar 13, 2019
Iterative Ruby(ish) Interpreter
View iterative_ruby_interpreter.rb
# helper
def seq(seq)
seq.reverse.reduce do |rest, crnt|
Parser::AST::Node.new(:seq, [crnt, rest])
end
end
# convert parser's AST to our AST
require 'parser/ruby25'
def parse(code)
@JoshCheek
JoshCheek / iterative_ruby_interpreter.rb
Created Mar 13, 2019
Iterative Ruby(ish) Interpreter
View iterative_ruby_interpreter.rb
# helper
def seq(seq)
seq.reverse.reduce do |rest, crnt|
Parser::AST::Node.new(:seq, [crnt, rest])
end
end
# convert parser's AST to our AST
require 'parser/ruby25'
def parse(code)
View another_async_ruby_example.rb
class Async
ignore_start = __LINE__
def initialize
@fibers = []
end
def branch(&block)
@fibers << Fiber.new(&block)
self
end
@JoshCheek
JoshCheek / terrible_equality_algorithm.js
Created Mar 11, 2019
JavaScript equality algorithm is terrible.
View terrible_equality_algorithm.js
class User {
constructor(name, age) { this.name = name, this.age = age }
[Symbol.toPrimitive](hint) {
// hint = "default", regardless of whether we compare to string or number
// ...also, how is an object not a primitive? Its got its own literal syntax ffs
console.log({args: [...arguments]})
return this.name // we dont know what we need, we just guess here
}
}
@JoshCheek
JoshCheek / 00.md
Last active Mar 7, 2019
Rainy Pond
View 00.md

You can view the final version here

View async_ruby.rb
# some of the syntax requires Ruby 2.5+
module Async
refine BasicObject do
def async(&block)
Async::Context.new(&block).run!
end
def enqueue(&block)
::Fiber.yield [:append, block]
end
You can’t perform that action at this time.