Skip to content

Instantly share code, notes, and snippets.

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

Stephen Best bestie

❤️‍🔥
View GitHub Profile
class DateRangeFilter
def self.to_proc
->(constraint, events) { new(constraint, events).call }.to_proc
end
def initialize(constraint, events)
@constraint = constraint
@events = events
end
@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.
@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 / application_controller.rb
Last active June 21, 2016 08:57
"Improve your code with dependency injection" code samples
class ApplicationController < ActionController::Base
private
def app
APP
end
end
@bestie
bestie / pre-commit
Created September 16, 2011 20:28
Rails Git pre-commit hook for ensuring schema.rb and migration changes commit atomically
#!/usr/bin/env ruby
# vim: set syntax=ruby
# Ensures that changes to the Rails schema.rb file may only be committed if a
# migration file is also committed at the same time.
def schema_modified?
%x[ git diff --cached |grep schema.rb ] == ''
end
@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 => " ",
}

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 / 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
@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 / 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,
]
}