Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / pipeline.rb
Created June 29, 2021 15:45
Ruby pipeline, based on JS example
# https://babeljs.io/blog/2018/07/19/whats-happening-with-the-pipeline-proposal
module Pipeable
def |(fn)
fn.to_proc.call self
end
refine(Enumerable) { include Pipeable }
refine(Proc) { include Pipeable }
refine(Array) { include Pipeable }
@JoshCheek
JoshCheek / example.rb
Created June 28, 2021 22:15
Ruby's three dot arguments misreport their parameter types
p RUBY_VERSION: RUBY_VERSION
def ab(a, b:)
[a, {b: b}]
end
puts "Three dots:"
def dots(...) ab(...) end
pp real_sig: method(:ab).parameters,
wrapper_sig: method(:dots).parameters,
@JoshCheek
JoshCheek / f.cpp
Last active June 26, 2021 18:24
Pretty sure C++ refs are just obfuscating syntax around pointers
// code for the screenshot in this tweet https://twitter.com/josh_cheek/status/1408853467150663680
#include "stdio.h"
void inc1(int& n) { ++ n; }
void inc2(int* n) { ++*n; }
int main(int argc, char** argv) {
int a = 0, b=0;
printf("a=%d, b=%d\n", a, b);
inc1(a); inc2(&b);
@JoshCheek
JoshCheek / module2_examp.rb
Created June 24, 2021 11:19
Example of how modules could keep track of what code to run on instances and what to run on classes and then generally work correctly whether extended or included
# An example to help me explain my thoughts in this tittwer convo:
# https://twitter.com/dorianmariefr/status/1407972746093641732
#
# Note that I ran this code on MRI 3.0.1, I assume it works on other versions too, but I did not try it.
# -----------------------------------------------------------------
# For simplicity, we'll only deal with keyword args in this example,
# I initially wrote it to deal with ordinals and blocks, but that got pointlessly dense
@JoshCheek
JoshCheek / yaml_boolean_values.fish
Last active June 20, 2021 22:07
YAML boolean values
# Context: https://twitter.com/josh_cheek/status/1406730934880198664
# NOTE: If you want to run this in bash, you have tod ouble-escape the newlines being passed to `tr`
curl -sL http://yaml.org/type/bool.html | # grab the docs on booleans
nokogiri -e 'puts $_.css("pre.screen").text' | # extract the values they mention
tr -d \n | # remove their whitespace formatting
tr \| \n | # each BNF "or" option gets its own line
ruby -r yaml -lne 'p [$_, YAML.load($_)]' | # print each input value and what it parses to
bat -l ruby # syntax highlight it
@JoshCheek
JoshCheek / 1.generate.rb
Last active June 8, 2021 21:54
Edit the secret message in encrypt.rb, run it, tell me what it prints
# I used this to generate the public / private keypair and sign them
require 'base64'
require 'openssl'
private_key = OpenSSL::PKey::RSA.generate 1024
public_key = private_key.public_key
signature = private_key.sign
OpenSSL::Digest::SHA256.new,
public_key.to_pem
)
@JoshCheek
JoshCheek / yegges-phone-screening-questions.rb
Created May 26, 2021 08:21
Steve Yegge's phone screen questions
https://sites.google.com/site/steveyegge2/five-essential-phone-screen-questions
# Area Number One: Coding
# 1. Write a function to reverse a string.
echo -e a\\nab\\nabc\\nabcdef© | ruby -lne 'puts $_.reverse'
# 2. Write function to compute Nth fibonacci number
seq 0 10 | ruby -ne '
a, b = 0, 1
$_.to_i.times { a, b = a+b, a }
@JoshCheek
JoshCheek / case_when.rb
Created May 16, 2021 05:08
Some ways to pass mutant tests
# alternative solutions to https://blog.arkency.com/semantic-blind-spot-in-ruby-case-statement/
class CaseWhen
def call(number)
boundaries = [0, 4, 8, 11]
messages = ['low value', 'medium value', 'high value']
boundaries.each_cons(2).zip messages do |(low, high), desc|
return desc if low <= number && number < high
end
'invalid value'
end
@JoshCheek
JoshCheek / object_referrers.rb
Last active May 6, 2021 02:17
Finding an object's referrers
require 'objspace'
require 'fiddle'
require 'json'
def heap
$heap ||= ObjectSpace
.dump_all(output: :string)
.lines.map { JSON.parse _1, symbolize_names: true }
.reject { |obj| %w[ROOT IMEMO DATA].include? obj[:type] } # probably overzealous
end
@JoshCheek
JoshCheek / 0.sh
Created May 5, 2021 23:02
Tracking allocated objects
echo Josh | ruby -r ./tracer.rb -e '
class User
def initialize(id, name)
@id, @name = id, name
end
def to_s
"User #@id: #@name"
end
end