Skip to content

Instantly share code, notes, and snippets.

👨‍💻
Hacking beautiful code...

Carlos Antonio da Silva carlosantoniodasilva

👨‍💻
Hacking beautiful code...
Block or report user

Report or block carlosantoniodasilva

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View deep_config.rb
require 'active_support/all'
class RailsConfig
class Options < ActiveSupport::OrderedOptions
def initialize(value = nil)
value.each_pair { |k, v| set_value k, v } if value.is_a?(Hash)
end
def method_missing(meth, *args)
if meth =~ /=$/
@carlosantoniodasilva
carlosantoniodasilva / devise.pt-BR.yml
Created Jun 10, 2014
Devise translations to pt-BR.
View devise.pt-BR.yml
pt-BR:
devise:
confirmations:
confirmed: 'Sua conta foi confirmada com sucesso.'
send_instructions: 'Dentro de minutos, você receberá um e-mail com instruções para a confirmação da sua conta.'
send_paranoid_instructions: 'Se o seu endereço de e-mail estiver cadastrado, você receberá uma mensagem com instruções para confirmação da sua conta.'
failure:
already_authenticated: 'Você já está logado.'
inactive: 'Sua conta ainda não foi ativada.'
invalid: 'E-mail ou senha inválidos.'
View while-vs-each_with_index.rb
require 'benchmark/ips'
ARRAY = [1,2,3,1,'2',4,'5',6,7,8,9,'10']
Benchmark.ips do |x|
x.report("while") {
hash = {}
index = 0
length = ARRAY.length
View tclient
#!/usr/bin/env ruby
require 'socket'
test_file = ARGV[0]
socket = UNIXSocket.new('testing.sock')
socket.write(test_file)
socket.close_write
@carlosantoniodasilva
carlosantoniodasilva / ar-override-define_attribute_methods.rb
Last active Dec 13, 2015
Test AR monkey patch of define_attribute_methods.
View ar-override-define_attribute_methods.rb
require 'active_record'
require 'active_record/base'
module ActiveRecord
module CVE123456
def define_attribute_methods
# ....
raise "OMG Monkey Patch!"
end
end
@carlosantoniodasilva
carlosantoniodasilva / simple_form_hour_input.rb
Created Jan 25, 2013
SimpleForm hour select input example.
View simple_form_hour_input.rb
# app/inputs/hour_input.rb
class HourInput < SimpleForm::Inputs::Base
def input
@builder.select(attribute_name, hour_options, { :selected => selected_value }, { :class => "input-medium" })
end
private
# The "Selecione..." string could also be translated with something like: I18n.t("helpers.select.prompt')
def hour_options
View bench_hash_key_vs_fetch.rb
require 'benchmark'
TIMES = 100000
HASH = { as: :fuu }
Benchmark.bm(25) do |x|
x.report('[:as] (existing)') do
TIMES.times do
HASH[:as] || :default
end
@carlosantoniodasilva
carlosantoniodasilva / infinite_comparable.rb
Last active Dec 10, 2015
Infinite Comparable attempt to refactor.
View infinite_comparable.rb
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
View bigdecimal-perf.rb
require 'benchmark/ips'
require 'bigdecimal'
Benchmark.ips do |x|
x.report("== true") {
BigDecimal('3') == true
}
x.report("TrueClass") {
TrueClass === BigDecimal('3')
View webrick_httpresponse_patch.rb
# 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:
You can’t perform that action at this time.