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
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
# 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
# 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("") |
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
# within your vhost... | |
RewriteEngine On | |
# create a variable called 'ps' which stores the original scheme | |
RewriteCond %{HTTPS} =on | |
RewriteRule ^(.+)$ - [env=ps:https] | |
RewriteCond %{HTTPS} !=on | |
RewriteRule ^(.+)$ - [env=ps:http] | |
# if the hostname (in this case, including the subdomain) does not match our desired hostname |
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
# is this not already provided somewhere? | |
# even_distribution?([50,49,53]) => true | |
# even_distribution?(50,33,70]) => false | |
def even_distribution?(array, slack = 0.1) | |
exact_distribution = 1 / array.size.to_f | |
slack_val = exact_distribution * slack | |
ceil = exact_distribution + slack_val | |
floor = exact_distribution - slack_val | |
total = array.reduce{ |v,t| v + t }.to_f | |
array.each do |element| |
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 http://www.nitrxgen.net/factorialcalc.php | |
The resulting factorial of 4,000! is 12,674 digits long. | |
The result also contains 999 trailing zeroes (which constitutes to 7.88% of the whole number) | |
182880195151406501331474317557391904421737771073043921970645269542089597979731773648503702868704841073364430415692855717546724618615435573339426156179 | |
569967167452848315973174988187609374828049804195765129487206105589281297880978006205934295377053267406244538842850917439517567461444473623787224694361 | |
945759295799001142129733606589980739777146972612050486637259363374904040660979666371702540213488009442803422853559466496813162601634597438035771759033 | |
947331700768417647790821668911845293242300334141454978018325982185184065522570973925300245827389829191044067821687088714956035019058673999662987985348 | |
777479231791957914165044080548789747703086507071208788376249865760733404494148545783673833017157063581941274008498556040804733051968334824080794209642 |