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 / 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 / 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 / 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:
require 'active_support/all'
class A
class_attribute :attrs
self.attrs = {}
end
class B < A
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)
@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 / 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 / gist:640361
Created October 22, 2010 11:08
Configure User Agent for selenium webdriver in Capybara
# Re-register selenium driver to be able to pass in the profile option, overriding the user agent.
Capybara.register_driver :selenium do |app|
require 'selenium/webdriver'
profile = Selenium::WebDriver::Firefox::Profile.new
profile['general.useragent.override'] = 'FooBar'
Capybara::Driver::Selenium.new(app, :profile => profile)
end
@carlosantoniodasilva
carlosantoniodasilva / bigdecimal-perf.rb
Created January 3, 2013 12:47
BigDecimal performance
require 'benchmark/ips'
require 'bigdecimal'
Benchmark.ips do |x|
x.report("== true") {
BigDecimal('3') == true
}
x.report("TrueClass") {
TrueClass === BigDecimal('3')
@carlosantoniodasilva
carlosantoniodasilva / infinite_comparable.rb
Last active December 10, 2015 14:38
Infinite Comparable attempt to refactor.
define_method '<=>_with_infinity' do |other|
if other.class == self.class
public_send('<=>_without_infinity', other)
else
infinite = try(:infinite?)
other_infinite = other.try(:infinite?)
# inf <=> inf
if infinite && other_infinite
infinite <=> other_infinite