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 / bench-sort.rb
Last active August 29, 2015 13:56
Benchmarking sort strategies in Ruby.
require 'benchmark'
a = Array.new(10_000) { rand }
Benchmark.bm(20) do |bench|
bench.report('sort <=>') { 10.times { a.sort { |x,y| y <=> x } } }
bench.report('sort.reverse') { 10.times { a.sort.reverse } }
bench.report('sort_by.reverse') { 10.times { a.sort_by(&:to_f).reverse } }
end
@timuruski
timuruski / bash_profile
Created March 12, 2014 14:20
Portable bash profile with a nice prompt
# PROMPT
# ==========
PS1="\[\e[33m\]\h\[\e[0m\]:\[\e[4m\]\W\[\e[0m\] \$ "
# ALIASES
# =========
alias ls="ls -lh"
@timuruski
timuruski / squeeze.rb
Created March 21, 2014 16:59
Not sure what to call this type of operation.
# Input
events = [
event(status: 'IDLE', job: 1, date: '2014-01-01'),
event(status: 'BUSY', job: 1, date: '2014-01-02'),
event(status: 'BUSY', job: 1, date: '2014-01-03'),
event(status: 'BUSY', job: 2, date: '2014-01-04'),
event(status: 'BUSY', job: 2, date: '2014-01-05'),
event(status: 'BUSY', job: 2, date: '2014-01-06'),
event(status: 'IDLE', job: 2, date: '2014-01-07'),
event(status: 'BUSY', job: 3, date: '2014-01-08'),

The ABCs of Ruby

  • A is for ARGF
  • B is for blocks
  • C is for CSV
  • D is for DBM (Database Management library) or Dir or DATA
  • E is for Enumerable and Enumerators
  • F is for Fibers
  • G is for GServer
  • H is for Hash
@timuruski
timuruski / migratory-habits.markdown
Last active August 29, 2015 13:58
Notes for a talk about Rails migrations

The Migratory Habits of Rails Developers

Table of contents:

  • migration tasks
  • new change syntax
  • using models in a migration
  • reversible migrations
  • revert command
#! /usr/bin/ruby
LBRACE = '{'
RBRACE = '}'
LBRACKET = '['
RBRACKET = ']'
COMMA = ','
COLON = ':'
WHITESPACE = /\s/
@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!"
@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)
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
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)