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
#!/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: |
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 |
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 |
class Foo | |
end | |
def Foo | |
Foo.new | |
end | |
p Foo.class | |
p Foo.new.class | |
p Foo().class |
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) |
" 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> |
#!/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' |