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
public void testSnappyHappy() { | |
Map snappy = null; | |
Map happy = new HashMap(); | |
snappy = addToMap(snappy, "foo", "bar"); | |
happy = addToMap(happy, "foo", "bar"); | |
} |
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
# From Alex Chaffee | |
# http://pivotallabs.com/users/alex/blog/articles/865-monkey-patch-du-jour | |
# Add caller line number to sql logging | |
module ActiveRecord | |
module ConnectionAdapters | |
class AbstractAdapter | |
def log_info(sql, name, ms) | |
if @logger && @logger.debug? | |
c = caller.detect{|line| line !~ /(activerecord|active_support|__DELEGATION__)/i} | |
c.gsub!("#{File.expand_path(File.dirname(RAILS_ROOT))}/", '') if defined?(RAILS_ROOT) |
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 RailsNamingConventions | |
def file_name_to_class_name(file_name) | |
file_name.split(".rb")[0].classify | |
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
class Test::Unit::TestCase | |
def assert_each(assertions) | |
error_messages = "" | |
assertions.each do |assertion, message, *variables| | |
template = message.nil? ? "<?> is not true." : message | |
if variables.empty? | |
msg = build_message(nil, template, assertion) | |
else | |
msg = build_message(nil, template, *variables) |
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
The interesting world of the asterisk in Ruby: | |
Of course we can multiply with it and it's quite handy in a regex, but there are two other interesting uses. | |
First, variable arguments in the method signature: | |
def hmm(*args) | |
first, *rest = *args | |
puts first.inspect | |
puts rest.inspect |
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 ActiveRecord::Base | |
def self.table_name_prefix | |
"appname." | |
end | |
end | |
class TransactionalBase < ActiveRecord::Base | |
self.abstract_class = true | |
establish_connection "#{RAILS_ENV}-transactional" | |
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 Hospital < AcitveRecord::Base | |
has_many :hospital_doctors | |
# Note: has_many_elsewhere is provided by the st-elsewhere gem | |
has_many_elsewhere :doctors, :through => :hospital_doctors | |
end | |
class HospitalDoctor < ActiveRecord::Base | |
belongs_to :hospital | |
belongs_to :doctor | |
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
# Posting XML using net/http in ruby | |
# This request will hang until it times out (defaulting to 60 seconds) | |
uri = URI.parse(SOME_SERVICE_URL) | |
response, data = Net::HTTP.start(uri.host, uri.port) do |h| | |
h.post(uri.path, some_xml) | |
end | |
# This works as expected | |
uri = URI.parse(SOME_SERVICE_URL) |
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 bash | |
# commit-derby | |
# Display a list of committers with the number of commits they have made in the last 30 days | |
# | |
COMMITTERS=`git log --format='%an' --since='30 days ago' | sort | uniq` | |
echo "In the last month, we've seen:" | |
for COMMITTER in ${COMMITTERS}; do | |
COMMITS=`git log --format='%an' --since='30 days ago' | sort | grep ${COMMITTER} | wc -l` |
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
# Ever forget the hostname of that server you logged into last week? | |
# Quickly get a chronological listing of hosts you've come to know... | |
require 'rubygems' | |
require 'net/ssh' | |
def who_do_i_know? | |
who = [] | |
known_host_files = Net::SSH::KnownHosts.hostfiles({}) | |
scanner = StringScanner.new("") |