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 / 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)
@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 / 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 / string_validation.rb
Last active December 21, 2022 17:20
Tip: RegExp implements case equality.
# If you want to check whether a string matches a regular expression,
# but not return the match data, you can just use === the case equality operator.
# Bad
!! ('foobar' =~ /bar/) #=> true
!! ('foo' =~ /bar/) #=> false
# Good
/bar/ === 'foobar' #=> true
/bar/ === 'foo' #=> false
@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
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 / 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 / pre-commit.rubocop
Last active February 1, 2019 13:10
Pre-Commit Rubocop check
#!/bin/sh
# Simplified from this script, (less robust and more to the point):
# http://gmodarelli.com/2015/02/code-reviews-rubocop-git-pre-commit/
# Installation: copy this code into <REPO>.git/hooks/pre-commit.sh
# Select only staged Ruby files
FILES="$(git diff --cached --name-only --diff-filter=AMC | grep "\.rb$" | tr '\n' ' ')"