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 / delta_t.rb
Created January 20, 2017 22:35
Script for dumping the time difference between a bunch of log lines.
#!/usr/bin/env ruby
require 'time'
# USAGE: This takes a bunch of lines as input, looks for dates, sorts them and
# then outputs the difference between the smallest and largest.
#
# pbpaste | ruby delta-t
# tail -f log/development.log | ruby delta-t
#
# TODO:
@timuruski
timuruski / query_cache_example.rb
Created January 19, 2017 19:32
Two possible ways to implement a basic in-memory cache for DB queries. One is more idiomatic, but I wonder if it's too idiomatic.
class UserImporter < ImporterBase
def import_user
user_name = @data[:user_name]
# Which is easier to understand?
users_by_name[user_name]
# -- OR --
find_user_by_name(user_name)
end
@timuruski
timuruski / README.md
Created December 2, 2016 02:41
Greeter script structure example

Structure of Ruby Scripts

This is an example of how to structure one-off Ruby scripts for easy growth, without making a big mess.

For more details on this, you can read my blog post about the idea or Confident Ruby by Avdi Grimm

#! /bin/bash
set -e
bold=$(tput bold)
normal=$(tput sgr0)
WORKSPACE="${HOME}/workspace"
PROJECTS=(
dotfiles
@timuruski
timuruski / foo.rb
Created March 4, 2016 16:18
Ruby is weird sometimes
class Foo
end
def Foo
Foo.new
end
p Foo.class
p Foo.new.class
p Foo().class
@timuruski
timuruski / box.sh
Created December 19, 2015 20:38
Draw a box!
echo "\u250C\u2500\u252c\u2500\u2510\n\u251C\u2500\u253C\u2500\u2524\n\u2514\u2500\u2534\u2500\u2518"
class TanksForAllTheFish < RTanque::Bot::Brain
NAME = 'Tanks For All the Fish'
MAX_RANGE = 400.0
include RTanque::Bot::BrainHelper
def tick!
if targets_nearby?
command.speed = 2.0
command.heading = Random.rand(-1.0..1.0)
@timuruski
timuruski / vimrc
Created July 16, 2015 18:14
Vim RunCmd primer
" Usage: To execute a Ruby file with some library included
" :RunCmd ruby -i lib/my_lib
" Then you can run your script by pressing <leader>r
command! -nargs=1 RunCmd nnoremap <leader>r :w \| !clear; <args> % <CR>
@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' ' ')"
#! /usr/bin/env ruby
# Not guaranteed to work.
require 'fileutils'
require 'pathname'
root = Pathname.new %x(git rev-parse --show-toplevel)
podfile_path = root + 'Podfile.lock'
pods_path = root + 'Pods'