This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''=~( '(?{' .('`' |'%') .('[' ^'-') | |
.('`' |'!') .('`' |',') .'"'. '\\$' | |
.'==' .('[' ^'+') .('`' |'/') .('[' | |
^'+') .'||' .(';' &'=') .(';' &'=') | |
.';-' .'-'. '\\$' .'=;' .('[' ^'(') | |
.('[' ^'.') .('`' |'"') .('!' ^'+') | |
.'_\\{' .'(\\$' .';=('. '\\$=|' ."\|".( '`'^'.' | |
).(('`')| '/').').' .'\\"'.+( '{'^'['). ('`'|'"') .('`'|'/' | |
).('['^'/') .('['^'/'). ('`'|',').( '`'|('%')). '\\".\\"'.( | |
'['^('(')). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Enumerable | |
def all_equal? | |
inject { |a,b| break false unless a == b; b } != false | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> Foo = 3 | |
=> 3 | |
>> class Bar; end | |
=> nil | |
>> module Fred; end | |
=> nil | |
>> Fred::Foo | |
NameError: uninitialized constant Fred::Foo | |
from (irb):4 | |
>> Bar::Foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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| |
OlderNewer