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
# Generates combinations of letters or numbers, or anything really. The combinations
# include repeating values, unlike Ruby's regular Array#permutation method.
#
# Usage:
# generate('a'..'c', 3) do |seq|
# puts seq.inspect #=> [a,a,a], [a,a,b], [a,a,c], [a,b,a]...
# end
def generate(range, comb_len)
range = range.to_a
range_len = range.length
@timuruski
timuruski / git_sred_log.sh
Last active August 29, 2015 14:08
Git SRED helper.
# In your .gitconfig add this alias:
# [alias]
# sred = "!source ~/.git_sred_log && sred_log"
# Assumes input date is formatted as YYYY-MM-DD,
# passes additional arguments to git-log.
sred_log() {
sred_date="${1:-$(date +"%Y-%m-%d")}"
shift
@timuruski
timuruski / compress.rb
Last active December 14, 2015 18:27
A dumb algorithm for compressing blocks of sorted integers, like row IDs.
# This algorithm takes a *sorted* list of integers, probably record
# IDs and compresses them into a series of ranges. It also includes a
# verification check that no values were lost.
def compress(ary)
range_start = ary.first
compressed = []
ary.each_with_index do |value, index|
# TODO: This could skip indices that have already been considered.
@timuruski
timuruski / README.markdown
Last active November 10, 2016 11:02
A dumb way to prettify JSON on the command line.

pjson

Prettifies JSON on the command line.

Installation

Copy into anywhere on $PATH and chmod +x the file. Remove the .rb if you like.

Usage

@timuruski
timuruski / myapp
Last active August 29, 2015 14:04
Replace bulky "git-like" libraries with a directory of scripts and an entry point.
#!/usr/bin/env bash
SCRIPT="script/myapp-${1}.sh"
shift
exec ${SCRIPT} ${@}
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)
class World
def initialize(cells)
@cells = cells
@row_count = cells.length
@col_count = cells[0].length
end
def living_neighbours(col, row)
[
cell(col, row - 1), # NORTH
@timuruski
timuruski / ruby_process.rb
Last active August 29, 2015 14:01
A mechanism for testing signals in Ruby processes.
class RubyProcess
WAIT_HELPER = <<-EOS
# Top-level method to signal the parent process
# and wait for an interrupt.
def wait_for(seconds)
# Signal parent we are ready.
Process.kill('USR1', Process.ppid)
# Wait for interrupt.
sleep(seconds)
@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 / press_any_key.rb
Last active August 29, 2015 14:01
Probably useless, but prompts the user to press any key and then blocks until input is entered. Useful for pausing a program while you go inspect something elsewhere, like a queue or DB or whatever.
require 'io/console'
class PressAnyKey
MESSAGE = 'Press any key to continue...'
def initialize(console: nil, message: MESSAGE)
console ||= File.open('/dev/tty', 'w+')
console.puts message
console.raw { |io| io.readchar rescue nil }
rescue
warn "Couldn't connect to console!"