Skip to content

Instantly share code, notes, and snippets.

View timuruski's full-sized avatar
🏳️‍⚧️
Protect Trans Rights

Tim Uruski timuruski

🏳️‍⚧️
Protect Trans Rights
View GitHub Profile

OLD SCHOOL

"How to kick it old school in the terminal."

You don't need a fancy web app to draw things on a screen and you don't need a bunch of gems to write a good command-line app. With some standard tools and a little bit of know-how you can grow your unix neckbeard and create old school programs that work anywhere Ruby does.

def trap_signals
[:INT, :QUIT].each do |signal|
trap(signal) do
# The \r omits the ^c usually produced by Ctrl-C for interrupt.
print "\rExiting...\n"
exit
end
end
end
#! /usr/bin/env ruby
require 'optparse'
require 'ostruct'
require 'time'
module SerialWorker
# Public: Run from command line arguments.
def self.run(args)
CommandLine.new(args).run
File.open(DATA) do |f|
if f.flock(File::LOCK_EX | File::LOCK_NB)
puts "Working..."
gets
else
puts "Job already started."
end
end
__END__
# Helpers for working with command line programs.
#
# Usage:
#
# subject { cli('echo foo bar' }
#
# it { should run_without_error }
# it { should produce_output_including('foo') }
# it { should produce_output_including(/bar/) }
#
require 'optparse'
require 'ostruct'
def parse_config(args)
config = OpenStruct.new
config.rails_env = ENV['RAILS_ENV'] || 'development'
parser = OptionParser.new do |opts|
opts.program_name = 'example'
opts.version = '1.0.0'

In Defense of Ugly Code

By Tim Uruski Thursday, 15 August 2013

Ugly code is offensive. But sometimes that's a good thing. Often when writing a test for some particularly egregious piece of code, I find myself tempted to do some acrobatics to make the test setup look nicer. But really, the problem is the design of the code that is making the test ugly. This is a sign pointing to the real problem. It's worthwhile to leave ugly code where it is and make it beautiful by cleaning up the problem causing the ugly code.

@timuruski
timuruski / shell-snippets.sh
Created August 12, 2013 15:32
Just some random bits of shell code that I sometimes forget.
find app -path '*.rb' | (while read filename; do wc -l $filename; done) | sort -r | head -50
find app -path '*.rb' | ruby -ne 'puts "#{$_.chomp.reverse}"'
@timuruski
timuruski / guard-exception.rb
Last active December 20, 2015 19:59
Which is the better place to raise a guard exception?
# This example will raise when we call ExampleA.new
# with an invalid ID.
# eg.
# ExampleA.new(1,2,3) # => raises "Foos were missing"
class ExampleA
def initialize(foo_ids)
@foos = find_foos(foo_ids)
end
def foobar
ack --nogroup "Rails.env." app | cut -d ':' -f'1,2' | ruby -ne "p,n = \$_.chomp.split(':'); blame = %x(git blame -L#{n},#{n} -- #{p}); puts %Q(#{p} -- #{blame})"