Skip to content

Instantly share code, notes, and snippets.

View carlosantoniodasilva's full-sized avatar
👨‍💻
Hacking beautiful code...

Carlos Antonio da Silva carlosantoniodasilva

👨‍💻
Hacking beautiful code...
View GitHub Profile
@carlosantoniodasilva
carlosantoniodasilva / webrick_httpresponse_patch.rb
Created November 12, 2012 15:31
Fix Webrick HTTPResponse warnings.
# WEBrick doesn't support keep alive connections for 204 and 304 responses. If a 204 or 304 response is made along with a keepalive, a warning is issued and webrick closes the connection.
# WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
# https://github.com/rails/rails/issues/3164
# https://bugs.ruby-lang.org/issues/5737
# ~/.rbenv/versions/1.9.3-pXXX/lib/ruby/1.9.1/webrick/httpresponse.rb
# Change line 205 to:
@carlosantoniodasilva
carlosantoniodasilva / benchmark_string_subst.rb
Created June 27, 2012 12:36
Benchmark string substitution with [] and gsub
require 'benchmark'
TIMES = 100000
s = "carlos"
Benchmark.bm(10) do |x|
x.report('[1...-1]') do
TIMES.times do
s.encode(s.encoding, :xml => :attr)[1...-1]
end
@carlosantoniodasilva
carlosantoniodasilva / include.rb
Created June 22, 2012 13:06
Ruby include order - from right to left
# Include works from right to left
module X
def self.included(base)
puts 'include X'
end
end
module Y
def self.included(base)
require 'active_support/all'
class A
class_attribute :attrs
self.attrs = {}
end
class B < A
end
@carlosantoniodasilva
carlosantoniodasilva / devise.pt-BR.yml
Created May 25, 2012 02:56 — forked from mateusg/devise.pt-BR.yml
pt-BR translations for Devise
# encoding: UTF-8
# pt-BR translations for Devise
pt-BR:
errors:
messages:
expired: "expirou, por favor, solicite uma nova"
not_found: "não encontrado"
already_confirmed: "já foi confirmado"
not_locked: "não foi bloqueado"
not_saved:
@carlosantoniodasilva
carlosantoniodasilva / dup.rb
Created May 13, 2012 20:58
Ruby dup/clone behavior
class A
def initialize_dup(other)
puts 'init dup'
super
end
def initialize_clone(other)
puts 'init clone'
super
end
@carlosantoniodasilva
carlosantoniodasilva / a.txt
Created April 29, 2012 20:28
Benches on PartialRenderer refactoring
## Running wycats/rails-simple-benches
Changes: https://github.com/carlosantoniodasilva/rails/compare/render-partial-refactor
### Master
$ time ruby minimal.rb
.........
overhead index template_1 partial partial_10 coll_10 partial_100 coll_100 uniq_100 diff_100
195 376 483 519 2144 1447 17997 11109 11438 14620
@carlosantoniodasilva
carlosantoniodasilva / worker_example_group.rb
Created February 15, 2012 22:59
RSpec - Register Example Group
module Support
module WorkerExampleGroup
include RSpec::Rails::RailsExampleGroup
def self.included(base)
base.metadata[:type] = :worker
end
end
end
@carlosantoniodasilva
carlosantoniodasilva / gist:1240679
Created September 25, 2011 14:54
Simple placeholder js
var Placeholder = {
init: function(selector) {
if (!this.supportPlaceholder()) {
$(selector).placeholder();
}
},
supportPlaceholder: function() {
return 'placeholder' in document.createElement('input');
}
}
@carlosantoniodasilva
carlosantoniodasilva / rspec_before_after_blocks.rb
Created April 14, 2011 17:55
Example showing how RSpec before/after blocks work.
# Acceptance spec
require "acceptance_helper"
feature "Rspec before blocks" do
before(:suite) do
puts "I'll run just once, before suite"
end
before(:all) do
puts "I'll run 3 times, before all"