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
require 'benchmark' | |
require 'benchmark/ips' | |
Benchmark.ips do |r| | |
r.report("group_by") do | |
(0..1000).group_by { |w| w % 2 == 0 }.values | |
end | |
EVEN = 0 | |
ODD = 1 |
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
update fb_audiences | |
set facebook_id = replace(facebook_id, '[^0-9]+$', ''); | |
SELECT id, facebook_id | |
FROM fb_audiences | |
WHERE facebook_id ~ '[^0-9]+$' |
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
(function() { | |
function hideBoringArticles(nodes) { | |
nodes.filter("article.badge-entry-container.badge-entry-entity").each(function(i, el) { | |
if($(el).data("entryVotes") < 20000) { | |
$(el).hide(); | |
} | |
}) | |
} | |
var MutationObserver = window.MutationObserver; |
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
def toss | |
(rand * 100) > 60 ? 1 : 2 | |
end | |
# Both players bet on 1 | |
def game | |
toss_1 = toss() | |
toss_2 = toss() | |
if toss_1 == 1 && toss_2 != 1 |
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
at_exit do | |
deploy.unlock | |
end | |
set :deploy_lock_file, "#{shared_path}/tmp/deploing_lock_file" | |
namespace :deploy do | |
desc "Check if somebody already is deploing" | |
task :check_lock do |
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 Array | |
def bindex element, lower = 0, upper = length - 1 | |
return if lower > upper | |
mid = (lower + upper) / 2 | |
element < self[mid] ? upper = mid - 1 : lower = mid + 1 | |
element == self[mid] ? mid : bindex(element, lower, upper) | |
end | |
end | |
class Array |
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 User | |
def say | |
puts "Hello" | |
end | |
end | |
def override_instance_methods(c) | |
c.instance_methods(false).each do |m| | |
c.class_eval <<-RUBY | |
alias #{m}_original #{m} |
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 String | |
def to_caesar shift | |
shift = (shift % 26).abs | |
each_byte.map { |b| ((b + shift - 97) % 26 + 97).chr }.join | |
end | |
end | |
a = "Caesar was a cool guy" | |
p a.split.map { |w| w.to_caesar 4 }.join(' ') # => "aeiwev aew e gssp kyc" |
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
h = {} | |
h[:label] = "Red" | |
h_copy = h | |
h_dup = h.dup | |
h_clone = h.clone | |
h_deep_copy = Marshal.load( Marshal.dump h ) | |
p h[:label].object_id # => 2156268500 | |
p h_dup[:label].object_id # => 2156268500 |
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
require 'test/unit' | |
include Test::Unit::Assertions | |
class Array | |
def transpose | |
return [] if empty? | |
a = Array.new(self.first.size) {[]} | |
each do |e| | |
begin |