Skip to content

Instantly share code, notes, and snippets.

View marcocarvalho's full-sized avatar

Marco Carvalho marcocarvalho

  • mcorp.io
  • Curitiba - PR - Brasil
View GitHub Profile
@marcocarvalho
marcocarvalho / ruby-gzip-alike.rb
Created July 2, 2013 19:50
Create gzip readable files using ruby zlib
fn = "filename"
compressed_file_name = "#{fn}.gz"
Zlib::GzipWriter.open(compressed_file_name) do |gz|
gz.mtime = File.mtime(fn)
gz.orig_name = fn
gz.write IO.binread(fn)
end
@marcocarvalho
marcocarvalho / hash.rb
Created September 3, 2012 13:06
Hash with Initial value
# Ao invés de fazer:
# dentro de uma iteração
h = {}
h[:somekey] = {} if h[:somekey].nil?
h[:somekey][:otherkey] = [] if h[:somekey][:otherkey].nil?
h[:somekey][:otherkey] << item_da_teracao
# Teremos um hash: { somekey: { :otherkey => [1, 2, 3, 4] } }
@marcocarvalho
marcocarvalho / combo_helper.rb
Created June 25, 2011 17:38
Rails 3 FormBuilder Select Helper that deals with association
#
# ~/app/helpers/combo_helper.rb
#
# Association => { { id: 1, association: "Coolest" } , { id: 2, association: "Hardest" } }
#
# Model Class << ActiveRecord::Base
# has_many :associations
# end
#
# Use: