This file contains hidden or 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
require 'rubygems' | |
require 'benchmark' | |
GC.disable | |
Benchmark.bm do |x| | |
x.report('Double Quotes') { 10000000.times { "[WARNING] This is really stupid." } } | |
x.report('Single Quotes') { 10000000.times { '[WARNING] This is really stupid.' } } | |
end |
This file contains hidden or 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
require 'benchmark' | |
class A | |
def test | |
100.times do | |
yield | |
end | |
end | |
end |
This file contains hidden or 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
# I hate this pattern, but it's something I have to deal with when parsing XML using HTTParty all the time. | |
class Object | |
def arrayify | |
self.is_a?(Array) ? self : [self] | |
end | |
end | |
hsh = { :name => 'Hashy Mustache' } | |
arry = [{ :name => 'Mason Arrayson' }] |
This file contains hidden or 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
require 'rubygems' | |
require 'benchmark' | |
test_array = %w(hamburger cheeseburger goatburger veggieburger lettuce tomato ketchup superburger) | |
Benchmark.bm do |x| | |
x.report { 1000000.times { test_array.grep(/burger/) } } | |
x.report { 1000000.times { test_array.select { |item| item.match(/burger/) } } } | |
end |
This file contains hidden or 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
require 'rubygems' | |
require 'date' | |
require 'active_support' | |
require 'benchmark' | |
test_time = "2011-01-16 22:48:03 EST" | |
Benchmark.bm do |x| | |
x.report { 100000.times { Time.parse(test_time) } } | |
x.report { 100000.times { DateTime.parse(test_time) } } |
This file contains hidden or 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 RedisLock | |
def lock_for_update(key, timeout = 60, max_attempts = 100) | |
if self.lock(key, timeout, max_attempts) | |
response = yield if block_given? | |
self.unlock(key) | |
return response | |
end | |
end | |
This file contains hidden or 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 Hash | |
def burgerize_keys! | |
self.keys.each { |key| self["#{key}_burger"] = self.delete(key) } | |
return self | |
end | |
end | |
class HashMapEnterpriseEdition < Hash | |
def burgerize_keys! |
This file contains hidden or 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
require 'rubygems' | |
require 'httparty' | |
class NFLStream | |
attr_accessor :current_scores | |
include HTTParty | |
format :json | |
base_uri "http://www.nfl.com" |
This file contains hidden or 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
def puts(s) | |
file = File.basename(caller.first) | |
super("puts() from #{file}: #{s}") | |
end | |
def print(s) | |
file = File.basename(caller.first) | |
super("print() from #{file}: #{s}") | |
end |
This file contains hidden or 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 Array | |
def map_with_index(&block) | |
counter = 0 | |
self.inject([]) { |mapped, item| mapped << block.call(item, counter); counter += 1; mapped } | |
end | |
end |