Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / looking_up_users_on_github.sh
Last active November 9, 2022 00:17
Looking up users on github
# 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 November 4, 2022 19:02
What even is a "class method"?
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 / tetris
Last active November 4, 2022 18:58
CLI 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 / interactive-grep.rb
Created November 2, 2022 05:50
Example of how you could create an interactive grep interface without a GUI
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
@JoshCheek
JoshCheek / bad_ideas.rb
Created November 1, 2022 11:28
bad ideas
# https://twitter.com/josh_cheek/status/1587406334839889921
$**?$ # => ""
$**%$*$ # => ""
$**%** # => ""
$**%$$ # => ""
$_ = {}
alias $*$_
public def *(*) = itself
alias ** *
@JoshCheek
JoshCheek / NameError::message.rb
Created October 27, 2022 17:27
NameError::message
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>
# https://twitter.com/josh_cheek/status/1583584074652082176
def
( class
class A
self
end::B < class C; self end
self
end
).something
'hello'
@JoshCheek
JoshCheek / example.bash
Last active October 18, 2022 01:39
How to get the same output as "detect" from bash
# https://twitter.com/inanna_malick/status/1582121657228922880
# files executable by the current user, with "detect" in the filename
# or files with a ".rs" extension, containing the string "map_layer"
find . \
\( -type f -perm -u=x -name '*detect*' -print \) \
-or \
\( -type f -name "*.rs" -exec grep --files-with-matches map_layer '{}' ';' \)
@JoshCheek
JoshCheek / hash2.rb
Created October 17, 2022 23:29
Alternate initialize for a hash, which is hopefully generally more user friendly
class Hash2 < Hash
def initialize(default_value=(use_proc=true; nil), &default_proc)
use_value = !use_proc
if use_value && default_proc
raise ArgumentError, "Provide a default value or a default proc, not both"
end
if use_value && !default_value.frozen?
raise ArgumentError, "Default values must be frozen"
end
@JoshCheek
JoshCheek / stateful_default_hash_values.rb
Last active October 17, 2022 22:33
Stateful default hash values
🤘 = Hash.new(Lol = Class.new)
🤘[🤘].define_method(:bbq) { 🤘[🤘] }
class Omg < 🤘[:😈]
instance_eval { alias wtf new }
end
Omg.wtf.bbq # => Lol