Skip to content

Instantly share code, notes, and snippets.

View CarlosCD's full-sized avatar

Carlos A. Carro Duplá CarlosCD

  • Firstup
  • Maryland, USA
View GitHub Profile
@CarlosCD
CarlosCD / power_addition.rb
Created May 20, 2015 01:14
Power as addition and iteration
# Question from Quora.com:
# How do I write a program that computes "b" raised to power "n" using only addition and iteration?
# http://www.quora.com/Computer-Programming/How-do-I-write-a-program-that-computes-b-raised-to-power-n-using-only-addition-and-iteration
# My solution in Ruby:
def power(b, n)
result = 1
n.times do
result = Array.new(b, result).reduce(:+).to_i
@CarlosCD
CarlosCD / config.ru
Created October 8, 2012 21:37
Delayed_job_web Basic Authentication filtering access by hostname and IP address
# Use SSL!
# And place in production.rb:
HOST_NAME = 'localhost:3000'
IP_ADDRESS = '10.17.1.125'
USER_NAME = 'johnny'
PASSSWORD = 'BeGood'
if Rails.env.production?
class SillyAuth < Rack::Auth::Basic
@CarlosCD
CarlosCD / thousands.rb
Last active December 27, 2015 19:19
Get a string that is the decimal representation of that number grouped by commas after every 3 digits. Like 1000 #=> '1,000'. --See the issues with some edge cases for floats.
# General case, English notation:
def pretty_number(num)
num.to_s.tap do |s|
:anything while s.gsub!(/^([^.]*)(\d)(?=(\d{3})+)/, "\\1\\2,")
end
end
# Puts the commas in the thousands, allows to use a character other than ',':
def pretty_number(number, comma = ',')
number.to_s.tap do |s|
@CarlosCD
CarlosCD / export_dbs.rb
Last active January 14, 2016 19:19
Migrating local PostgreSQL databases in my laptop, when upgrading the server (in this case 9.4 to 9.5)
#!/usr/bin/env ruby
# See: http://postgresapp.com/documentation/install.html
dumping_folder = 'dump'
expected_header = " List of databases\n" +
" Name | Owner | Encoding | Collate | Ctype | Access privileges \n" +
"-------------------------------------+--------+----------+-------------+-------------+-------------------\n"
databases = `psql --list`
@CarlosCD
CarlosCD / draper-mongoid.gemspec
Created January 27, 2016 17:48 — forked from johnbintz/draper-mongoid.gemspec
Ensure Mongoid documents work with Draper
Gem::Specification.new do |s|
s.name = 'draper-mongoid'
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.author = 'John Bintz'
s.email = 'john@coswellproductions.com'
s.summary = 'Quick little hack to make Mongoid work with Draper wrt model_name'
s.description = 'Quick little hack to make Mongoid work with Draper wrt model_name'
s.files = ['draper-mongoid.rb']
@CarlosCD
CarlosCD / mongoid-accepts_embedded_resource_for.gemspec
Created January 27, 2016 17:52 — forked from johnbintz/mongoid-accepts_embedded_resource_for.gemspec
Add accepts_embedded_resources_for to Mongoid::NestedAttributes
Gem::Specification.new do |s|
s.name = 'mongoid-accepts_embedded_resource_for'
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.author = 'John Bintz'
s.email = 'john@coswellproductions.com'
s.summary = 'Add accepts_embedded_resources_for to Mongoid::NestedAttributes, so it plays nicer with Carrierwave and embedded documents'
s.description = 'Add accepts_embedded_resources_for to Mongoid::NestedAttributes, so it plays nicer with Carrierwave and embedded documents'
s.files = ['mongoid-accepts_embedded_resource_for.rb']
@CarlosCD
CarlosCD / gist:ef55c0ca0b04a671d9be4ae3085bc61f
Last active January 4, 2018 18:52
Array#flatten in Ruby
# Array#flatten in Ruby (exercise).
# Added as an Array instance method, with the name 'flattening'
class Array
def flattening
flat_me(self)
end
private
@CarlosCD
CarlosCD / git_messages.sh
Created July 27, 2018 18:42
Analysis of words used in commit messages
# All by order of frequency:
git log --pretty=format:'%s' | cut -d " " -f 1 | sort | uniq -c | sort -nr
# Angry developers:
git log --pretty=format:'%s' | tr '[:upper:]' '[:lower:]' | tr '[:space:]' '\n' | sort | uniq -c | sort -nr | grep -iE "(fuck|shit|bitch|damn|piss)"
require 'nokogiri'
require 'open-uri'
# Get a Nokogiri::HTML:Document for the page we're interested in...
doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))
# Do funky things with it using Nokogiri::XML::Node methods...
####
@CarlosCD
CarlosCD / safe.md
Created October 2, 2020 15:29 — forked from bryanrite/safe.md
Safe Postgres Operations on High Volume Tables

Originally taken from: Braintree Article and expanded on by me.

Safe

  • Add a new column
  • Drop a column
  • Rename a column
  • Add an index concurrently (Example), Note: it will still take a long time to run the migration, but it won't write-lock the table.
  • Drop a constraint (for example, non-nullable)
  • Add a default value to an existing column