Skip to content

Instantly share code, notes, and snippets.

View bestie's full-sized avatar
❤️‍🔥

Stephen Best bestie

❤️‍🔥
View GitHub Profile
@bestie
bestie / thread_sleep_with_custom_state.rb
Last active September 7, 2022 10:22
Ruby Thread#stop / #Thread.stop? doesn't work how you might think.
# This does work because a my custom thread status is unambiguous.
#
# However, you may notice that setting the status and sleeping are not atomic.
require "net/http"
responses = []
thread = Thread.new do
Thread.current[:what_i_am_doing] = "starting"
@bestie
bestie / racknroll.ru
Created July 5, 2021 16:52
A Rack app for rick rollin'
body = <<~HTML
<html>
<body>
<div class="tenor-gif-embed" data-postid="12136175" data-share-method="host" data-width="100%" data-aspect-ratio="1.4777448071216617"><a href="https://tenor.com/view/rick-ashley-dance-80s-music-gif-12136175">Rick Ashley Dance GIF</a> from <a href="https://tenor.com/search/rickashley-gifs">Rickashley GIFs</a></div><script type="text/javascript" async src="https://tenor.com/embed.js"></script>
</body>
</html>
HTML
app = ->(env) {
[
@bestie
bestie / 1_udp_listener.rb
Last active September 25, 2019 15:17
Help me fix this Rust UDP problem :)
require "socket"
rx_port = 2222
tx_port = 2223
host = "localhost"
socket = UDPSocket.new
socket.bind(host, rx_port)
socket.connect(host, tx_port)
@bestie
bestie / prison_break.rb
Last active August 3, 2018 13:28
Prison Break
# Run this script with the ENV var payload.
# Payload will be executed as part of your request to visit a prisoner.
# The goal is to free a prison in as few characters as possible.
module PrisonBreak
class Visit
attr_accessor :free_prisoner
attr_reader :prison, :payload

Keybase proof

I hereby claim:

  • I am bestie on github.
  • I am bestie (https://keybase.io/bestie) on keybase.
  • I have a public key ASAU_62OgyyQ6IpGAlmQkLNK-JwXmslWMiOAueQy95ieOgo

To claim this, I am signing this object:

@bestie
bestie / password_gen.rb
Last active January 19, 2018 19:17
Memorable, secure password generator in one file. XKCD style, includes 1765 common words.
#!/usr/bin/env ruby
require "optparse"
options = {
:n_passwords => 1,
:password_length => 4,
:separator => " ",
}
@bestie
bestie / wat_gems.rb
Created November 28, 2017 14:16
Parse the Gemfile.lock file and print the list of Gems that Bundler would install
require "bundler"
bundle = Bundler::LockfileParser.new(Bundler.read_file(Bundler.default_lockfile))
gem_name_version_map = bundle.specs.map { |spec|
[
spec.name,
spec.version.to_s,
]
}
@bestie
bestie / count_colors.rb
Created June 15, 2016 20:13
Imperative `Hash.new` vs functional
# Task: Count how many users have each favorite color
User = Struct.new(:id, :favorite_color)
colors = [:red, :green, :blue, :hot_pink]
users = 10.times.map { |n| User.new(n, colors.sample) }
# The follow two example print the same result
@bestie
bestie / tsort_spec.rb
Last active March 29, 2016 21:39
A topological sort head scratcher
# Ah it's OK I got this one myself. It's pretty late at night but I get it now!
# I'll leave this here in case anyone is interested. I've added the
# explaination below
# This is some code I wrote as excercise after watching
# https://youtu.be/QnWDU1wcsPA?t=470
#
# The first example mimics the result from the video and is there just to
# verify my use of the TSort module is correct.
class DateRangeFilter
def self.to_proc
->(constraint, events) { new(constraint, events).call }.to_proc
end
def initialize(constraint, events)
@constraint = constraint
@events = events
end