Skip to content

Instantly share code, notes, and snippets.

View timuruski's full-sized avatar
🥊
Hit. Don't get hit.

Tim Uruski timuruski

🥊
Hit. Don't get hit.
View GitHub Profile
@timuruski
timuruski / minimal-railsrc
Created June 20, 2023 13:32
Minimal railsrc for experiments
--skip-action-mailer
--skip-action-mailbox
--skip-action-text
--skip-active-job
--skip-active-storage
--skip-action-cable
--skip-jbuilder
--skip-bootsnap
@timuruski
timuruski / notify_bugsnag.rb
Created September 2, 2021 22:05
Simple, composable Bugsnag matcher.
RSpec::Matchers.define :notify_bugsnag do |error = nil, message = nil|
supports_block_expectations
match do |block|
if error
expect(Bugsnag).to receive(:notify).with(instance_of(error))
else
expect(Bugsnag).to receive(:notify)
end
@timuruski
timuruski / module-collision.rb
Created August 23, 2021 21:48
An example of private methods colliding in modules.
module Foo
def foo
blah
end
private def blah
"foo"
end
end
@timuruski
timuruski / http_request_spec.rb
Created January 13, 2021 22:41
Example of a flexible pattern for making HTTP requests with Ruby Net:HTTP
require "json"
require "net/http"
require "uri"
# The standard library Net::HTTP module is a bit convoluted, but here is an example of a flexible
# system for making different kinds of HTTP requests. To see some convenience methods you can
# consult the docs: https://ruby-doc.org/stdlib-2.5.5/libdoc/net/http/rdoc/Net/HTTP.html
class HttpConnection
def get(url, params = nil)
def beep(a, b)
result = ""
result += a.to_s
if a && b
result += " and "
end
result += b.to_s
end
puts beep("foo", "bar")
@timuruski
timuruski / block_binding.rb
Last active September 8, 2023 19:46
Demonstration of block binding differences in Ruby
class Expectation
def to(matcher = nil, &block)
if block_given?
puts "block passed to `to` method"
yield
end
end
end
def expect
@timuruski
timuruski / time_parts.rb
Created August 7, 2020 22:15
Example of an algorithm to get years, months, days between two days (expressed in two ways)
require "time"
p now = Time.parse("2020-08-07")
p date = Time.parse("2013-08-07")
delta, _ = (now.to_i - date.to_i).divmod(24 * 60 * 60)
years, rest = delta.divmod(365)
months, days = rest.divmod(30)
puts "years: #{years}"
puts "months: #{months}"
@timuruski
timuruski / yay.rb
Created August 5, 2020 14:56
Sketch of a tool for searching YAML by key
#!/usr/bin/env ruby
require "psych"
require "pry"
# Handler for detecting scalar values
class YayHandler < Psych::Handler
# States:
# a -> start_map -> b
# b -> scalar -> c (push key)
@timuruski
timuruski / 10_rubocop.sh
Last active April 24, 2018 15:58
Modular pre-commit hook, script.
#!/bin/bash --login
# Select only staged Ruby files, this may be expanded to other file types if necessary.
FILES="$(git diff --cached --name-only --diff-filter=AMC | grep -E "\.(erb|rake|rb)$" | tr '\n' ' ')"
# Check for ruby style errors
if [ -n "$FILES" ]; then
bundle exec rubocop --force-exclusion --format=simple ${FILES}
fi
@timuruski
timuruski / .gitconfig
Last active March 23, 2017 22:12
Git Fuzzy/Interactive Checkout
[alias]
ci = "!source ~/.githelpers && fuzzy_checkout"