Skip to content

Instantly share code, notes, and snippets.

View rubysolo's full-sized avatar

Solomon White rubysolo

View GitHub Profile
headers = []
File.open("/path/to/file.tsv") do |file|
file.each_line do |line|
data = line.split(/\t/)
if headers.empty?
headers = data
else
data = Hash[*headers.zip(data).flatten]
MyModel.create(data)
@rubysolo
rubysolo / gist:1986444
Created March 6, 2012 14:03 — forked from codeincontext/gist:1831643
Upload a random twitter avatar from a directory
#!/usr/bin/env ruby
# Heavily based on https://github.com/ip2k/twitter-avatar-update
# ==== Gems ====
require 'twitter_oauth'
require 'oauth'
require 'nokogiri'
require 'open-uri'
require 'yaml'
@rubysolo
rubysolo / ar.rb
Created March 6, 2012 14:02 — forked from jbarnette/ar.rb
class ActiveRecord::Base
attr_accessible nil
def update_attributes *args
raise "Don't call #{self.class.name}#update_attributes. " +
"Mass assignment is pure evil."
end
end
class PrimeFactor
def initialize(number)
@factors = Fiber.new do
divisor = 2
while divisor < number
while number % divisor == 0
Fiber.yield divisor
number = number / divisor
end
divisor = divisor + 1
@rubysolo
rubysolo / code2png.rb
Created February 19, 2012 22:02 — forked from peterc/code2png.rb
code2png - Render code to an image on OS X
#!/usr/bin/env ruby
# code2png - Render code to an image on OS X
# Peter Cooper (@peterc)
# MIT license
#
# code2png converts source code into a PNG graphic
# (with syntax coloring, if you want). Ideal for
# Kindle document production, RSS feeds, etc.
#
@rubysolo
rubysolo / unicorn.sh
Created January 31, 2012 20:26 — forked from troex/unicorn.sh
Unicorn start/stop/restart script for debian / ubuntu
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: unicorn initscript
# Description: Unicorn is an HTTP server for Rack application
### END INIT INFO
cmd: >>>sudo -u foo PATH=/usr/local/bin:/usr/bin:/bin sh -c "cd /home/foo && pwd"<<<
/home/foo: 1: Syntax error: Unterminated quoted string
@rubysolo
rubysolo / gist:1590176
Created January 10, 2012 17:39 — forked from jstorimer/hilong.rb
hilong -- A simply utility to show character counts for each line of input and highlight lines longer than 80 characters.
#!/usr/bin/env ruby
# A simply utility to show character counts for each line of input and
# highlight lines longer than 80 characters.
#
# Written as an example for http://jstorimer.com/2011/12/12/writing-ruby-scripts-that-respect-pipelines.html
#
# Examples:
#
# $ hilong Gemfile
@rubysolo
rubysolo / criteria.rb
Created January 9, 2012 19:46
AR Criteria
class Invoice < ActiveRecord::Base
has_many :line_items
end
class LineItemTemplate < ActiveRecord::Base
has_many :criteria
end
class Criteria < ActiveRecord::Base
belongs_to :line_item_template
def output name=((default=true); "caius")
puts "name: #{name.inspect}"
puts "default: #{default.inspect}"
end
output
# >> name: "caius"
# >> default: true
output "avdi"