Skip to content

Instantly share code, notes, and snippets.

require 'date'
# Ruby implementation of algorithm for approximating
# sunrise and sunset for a given date, latitude, and longitude.
# Ported from instructions at http://users.electromagnetic.net/bu/astro/sunrise-set.php
# I think this is accurate to within about 10 minutes.
class SunriseSunset
def sunset
@sunset ||= begin
jd = (julian_sunset + 0.5).to_i
@outoftime
outoftime / pipenotify
Created January 13, 2009 22:25
Pipe stdin into libnotify, line by line
#!/usr/bin/env ruby
while line = STDIN.gets
puts line if ARGV.include?('-v')
system('notify-send', line)
end
@outoftime
outoftime / starping
Created January 13, 2009 22:42
Script to repetitively open connections with starling and report lag time or error
#!/usr/bin/env ruby
require 'rubygems'
gem 'starling-starling'
require 'starling'
unless ARGV.length == 1
STDERR.puts('Usage: starping <host>:<port>')
exit(1)
end
class CallLogged
def hey
puts 'hey'
end
def double(i)
i*2
end
public_instance_methods(false).each do |method|
@outoftime
outoftime / gist:54846
Created January 29, 2009 23:51
Tiny framework for one-line tests in Test::Unit
# Tiny framework for writing one-line tests. Just call expect with a block. The
# block evaluates to true if the test passes and false if it doesn't.
#
# Example:
# class ExpectTest < Test::Unit::TestCase
# include Expect
#
# expect { 1 == 1 }
# expect { 1 == 2 }
# end
class Airmigo
include DataMapper::Resource
property :id, Serial
belongs_to :from, :class_name => 'User'
belongs_to :to, :class_name => 'User'
before :save do
Airmigo.first_or_create(:conditions => { :from => to, :to => from })
#
# Usage:
# rake db:rollback_to_common branch=production
# git checkout -m production
# rake db:migrate
#
namespace :db do
desc 'Rolls back database to common migration state between current branch and another'
task :rollback_to_common do
FileUtils.cd(RAILS_ROOT) do
############### Named filter issues.
#
# 1) The table aliases for explicit joins are hacky. Obviously this is an easy fix, but we should
# talk about the best alternative. In this example, the resulting alias should not be "posts_blogs".
#
# => Agreed, although I can't think of anything really clever at the moment...
Post.filter do
join(Blog, :left, :posts_blogs) do
FixActiveRecordValidationsFullMessages.install
@outoftime
outoftime / rubyloc.rb
Created July 17, 2009 21:12
Count lines of Ruby code agnostic to the newline style
#!/usr/bin/env ruby
require 'rubygems'
require 'parse_tree'
require 'ruby2ruby'
count = 0
for file in ARGV
file_count = Ruby2Ruby.translate(IO.read(file), nil).count("\n")
count += file_count