Skip to content

Instantly share code, notes, and snippets.

View levinalex's full-sized avatar

Levin Alexander levinalex

View GitHub Profile
class Date
def minus_with_date(other)
if other.class == Date
(self.minus_without_date(other)).days
else
minus_without_date(other)
end
end
alias_method :minus_without_date, :-
alias_method :-, :minus_with_date
# return the date at which the week with the given number starts
# (ISO-8601)
#
# Date.beginning_of_numbered_week(2003, 52) # => 2003-12-22
# Date.beginning_of_numbered_week("2003W52") # => 2003-12-22
#
def self.beginning_of_numbered_week(year, week = nil)
if week
year, week = year.to_i, week.to_i
else
@levinalex
levinalex / gist:40995
Created December 28, 2008 17:27
99 bottles of beer
''=~( '(?{' .('`' |'%') .('[' ^'-')
.('`' |'!') .('`' |',') .'"'. '\\$'
.'==' .('[' ^'+') .('`' |'/') .('['
^'+') .'||' .(';' &'=') .(';' &'=')
.';-' .'-'. '\\$' .'=;' .('[' ^'(')
.('[' ^'.') .('`' |'"') .('!' ^'+')
.'_\\{' .'(\\$' .';=('. '\\$=|' ."\|".( '`'^'.'
).(('`')| '/').').' .'\\"'.+( '{'^'['). ('`'|'"') .('`'|'/'
).('['^'/') .('['^'/'). ('`'|',').( '`'|('%')). '\\".\\"'.(
'['^('(')).
module Enumerable
def all_equal?
inject { |a,b| break false unless a == b; b } != false
end
end
#
# Rubyquiz #65 (Splitting the Loot)
# Levin Alexander <levin@grundeis.net>
#
module Enumerable
def sum; inject { |a,b| a+b } end
def all_equal?
inject { |a,b| break false unless a == b; b } != false
# print a sorted list of all classes in the ruby vm sorted by count
#
def object_statistics(minimum = 2)
counts = {}
ObjectSpace.each_object do |o|
counts[o.class] = (counts[o.class] || 0) + 1
end
counts.to_a.reject {|k,v| v < minimum}.sort_by { |k,v| -v }.each { |k,v| puts "%6d " % v + k.to_s }
nil
end
#!/bin/bash
#
# some oddities with rails constant lookup
#
# "Foo::Bar" will return the toplevel constant "Bar" the first time it is referenced
# and fail the second time.
#
# (if Foo is a class instead of a module, the second call will print a warning and
# not raise a NameError)
#
>> Foo = 3
=> 3
>> class Bar; end
=> nil
>> module Fred; end
=> nil
>> Fred::Foo
NameError: uninitialized constant Fred::Foo
from (irb):4
>> Bar::Foo
#!/usr/bin/env ruby
#
# burning_down_lighthouse
#
# very crude munin plugin that shows current open lighthouse tickets by milestone
require 'rubygems'
require 'lighthouse'
require 'yaml'
#!/usr/bin/env ruby
#
# say a random word from the dictionary at least every ~n seconds
#
# uses "say" (comes with OSX) and growlnotify (comes with growl)
$f = File.open("/usr/share/dict/words")
$interval = ARGV[0].to_f || 0
$f.sort_by{rand}.each do |l|