Skip to content

Instantly share code, notes, and snippets.

JoshCheek / split_path_slashes_but_not_url_slashes.rb
Last active Nov 22, 2022
Split slashes in paths, but not URLs
View split_path_slashes_but_not_url_slashes.rb
def split_path(path)
slash = Regexp.escape File::SEPARATOR
match = path.match(%r~ # regex that is delimited by tildes so I can use slashes, quotes, backticks, etc in the comments
( # capture whatever the path_slash group matches
(?<path_slash> # a named group that matches slashes in paths, but not urls, eg will not match ""
# don't match first slash in "://"
(?: # don't capture this group
(?<!:) # not preceeded by ":"
(?!#{slash}#{slash}) # not succeeded by "//" (we haven't matched the slash yet, so it's in front of the cursor)
JoshCheek / semicolon-at-the-beginning.js
Created Nov 15, 2022
Example of putting the semicolon at the beginning of the line in JS
View semicolon-at-the-beginning.js
// this line will "continue" the previous one so
// it gets a semicolon to stop it from doing that
// JS would otherwise consider it a function call,
// ie `console.log("hello")(function() { ... })
;(function() {
JoshCheek / whydoesntgowork.go
Last active Nov 10, 2022
View whydoesntgowork.go
package main
import ("fmt")
// Real A (I cannot change this)
type RealA struct{}
func NewRealA() *RealA { return &RealA{} }
func (a *RealA) M() string { return "real a" }
// Fake A (I can change this)
type FakeA struct{}
JoshCheek /
Last active Nov 9, 2022
Looking up users on github
# imgcat is this program, it works with iTerm2:
# not sure how to make it responsive, you'd need a way to figure out how wide a character is, which may exist
# this number of spaces was determined experimentally and sadly it changes as you resize the terminal :shrug:
ruby -rshellwords -rjson -e ' { `curl --silent{_1.shellescape}` }
.map { JSON.parse _1 }
.map { <<USER.chomp }.join("\n").display
JoshCheek / what_even_is_a_class_method.rb
Created Nov 4, 2022
What even is a "class method"?
View what_even_is_a_class_method.rb
class A
def self.b # we all agree this is a class method
A.b # => "A.b"
class << A
def c # but what about this? it's the same thing, but we define it differently
JoshCheek / interactive-grep.rb
Created Nov 2, 2022
Example of how you could create an interactive grep interface without a GUI
View interactive-grep.rb
ruby -r io/console -e '
h, w = $stdout.winsize
options = ""
loop do
command = "grep -r #{options} ." # DO NOT INTERPOLATE INTO BASH SCRIPTS IN PROD!
results = `echo -n | #{command} | head -n #{h-3}`
print "\e[H\e[J\e[2B#{results}\e[H> #{command}\e[K\e[2D"
c = $stdin.getch
break puts "\e[H\e[J#{command}" if c == ?\C-c || c == "\r"
options += c # the basic-est of input processing
View bad_ideas.rb
$**?$ # => ""
$**%$*$ # => ""
$**%** # => ""
$**%$$ # => ""
$_ = {}
alias $*$_
public def *(*) = itself
alias ** *
View NameError::message.rb
require "objspace"
# A name error
err = omg rescue $!
err # => #<NameError: undefined local variable or method `omg' for main:Object>
# The error has a message object instead of a message string
# this object is a secret internal class with an invalid constant name
_, message, * = ObjectSpace.reachable_objects_from err
message # => #<NameError::message:0x000000015590b608>
JoshCheek / golang-test-wrapper
Last active Nov 22, 2022
Golang test runner (not very mature, prob lots of cases I don't yet know about, that it doesn't handle correctly yet)
View golang-test-wrapper
#!/usr/bin/env ruby
require 'json'
require 'time'
require 'pp'
# Run from the root of the app
Dir.chdir File.dirname __dir__
# Find package information for handling args
ROOT_PACKAGE = File.foreach("go.mod").first.split.last
View dynamic_class_namespaces_and_superclasses_and_singleton_classes.rb
( class
class A
end::B < class C; self end