Skip to content

Instantly share code, notes, and snippets.

@jmmastey
jmmastey / rules_and_examples.rb
Last active October 29, 2021 14:51
Score a Bowling Game
# Our goal today is to write a program (or just a method) that returns the total score
# for a game of bowling. We'll start with simple examples, but the rules can get quite
# confusing. The goal of the interview is to talk through decisions and dealing with
# new requirements.
#
# You very probably won't get through every example before time is done. That's expected.
#
# Any language is fine, as is either object oriented or procedural code.If you don't
# know the basics of how bowling works, you should ask up front.
@jmmastey
jmmastey / .rails_utils.sh
Last active June 8, 2021 15:42
custom development commands
# Railsy Utilities
# To use these, move this file into your home directory as .rails_utils.sh
# and add the following to your ~/.zshrc or ~/.bashrc
#
# source ~/.rails_utils.sh
#
# Usage: Navigate your terminal to the source code of any gem in your bundle,
# specifically the correct _version_ of that gem.
#
# Add these to your ~/.zshrc to expose them in any console. Both commands use the same
# general API. Navigate to a project that has a Gemfile (prolly mealhand), and run the command. It'll grab
# the correct version of the gem automatically!
# navigates your shell to the ~/.gem/ruby-2.6.1/whatever path where the gem lives
function bcd() { cd $(bundle info "$@" | grep -e "Path: [^\S]*" | sed -e 's/^[^/]*//') ;}
# opens up the "homepage" for the gem in your browser. typically github
function bweb() {
curl https://rubygems.org/api/v1/gems/"$@".json 2>/dev/null > /tmp/bweb
christmas_movies = {
die_hard: { violence: true, cheer: true },
nightmare: { violence: false, cheer: 'who knows' }
}
# if I'm just digging one layer into the hash, I don't get an error,
# I just get nil.
christmas_movies[:die_hard] # => { violence: true, cheer: true }
christmas_movies[:home_alone_3] # => nil
@jmmastey
jmmastey / .pryrc
Last active January 29, 2021 16:33
# Put this code (or whatever part is useful) in a file in your
# home directory called `~/.pryrc`. Then, every time you load
# a pry console, this code will automatically be executed, giving
# you these tools! I write a lot of little scripts this way.
# see `ar_counts` below
class ArCounter
def initialize
@last_count = 0
@current_count = 0
@jmmastey
jmmastey / monkey_patch.rb
Created March 26, 2019 13:39
codebase upgrades
if Gem::Version.new(Rails.version) >= Gem::Version.new("5.0")
raise "Remove this file! You don't need it anymore"
end
class ActiveJob::Base
def self.deserialize(job_data)
# monkey patch goes here
end
end
class Struct
# use the members of this struct to pull out hash keys in the right order
def self.from_hash(hash)
new(
*hash.symbolize_keys
.values_at(*members),
)
end
end
opts = {}
str = '{{ options.foo__required }}{{ options.bar__required }}{{ options.baz_9 }}'
# I'm still flattening right now... though just once in this case.
keys = str.scan(/\{\{ options.(\w+) \}\}/).flatten
=> ["foo__required", "bar__required", "baz_9"]
opts.merge!(Hash[keys.product([nil])])
=> {"foo__required"=>nil, "bar__required"=>nil, "baz_9"=>nil}
@jmmastey
jmmastey / keybase.md
Created April 23, 2018 16:28
keybase.md

Keybase proof

I hereby claim:

  • I am jmmastey on github.
  • I am jmmastey (https://keybase.io/jmmastey) on keybase.
  • I have a public key ASDq1RNE_EZKdZkKp64DNMLnFUlbianPiqRVqZ0iqbQI2go

To claim this, I am signing this object:

nodes
id   code              depth  parent
1    B                 0      {NULL}
2    B-123             1      1
3    B-123-BLAH        2      2
...
6    B-123-BLAH-1-2-3  6      5