Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
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 "http://example.com"
# 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
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
// https://twitter.com/josh_cheek/status/1592514535789060101
console.log(1)
// 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() {
console.log(2)
@JoshCheek
JoshCheek / whydoesntgowork.go
Last active Nov 10, 2022
whydoesntgowork.go
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
JoshCheek / looking_up_users_on_github.sh
Last active Nov 9, 2022
Looking up users on github
View looking_up_users_on_github.sh
# imgcat is this program, it works with iTerm2: https://github.com/JoshCheek/dotfiles/blob/main/bin/imgcat
# 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 '
ARGV.map { `curl --silent https://api.github.com/users/#{_1.shellescape}` }
.map { JSON.parse _1 }
.map { <<USER.chomp }.join("\n").display
#{_1["id"]}
#{_1["name"]}
@JoshCheek
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'
end
end
A.b # => "A.b"
class << A
def c # but what about this? it's the same thing, but we define it differently
'A.c'
@JoshCheek
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
# https://twitter.com/josh_cheek/status/1587406334839889921
$**?$ # => ""
$**%$*$ # => ""
$**%** # => ""
$**%$$ # => ""
$_ = {}
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
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
# https://twitter.com/josh_cheek/status/1583584074652082176
def
( class
class A
self
end::B < class C; self end
self
end
).something
'hello'