Skip to content

Instantly share code, notes, and snippets.

View marcomd's full-sized avatar

Marco Mastrodonato marcomd

View GitHub Profile
@marcomd
marcomd / select_and_map_VS_map_and_compact_VS_filter_map.rb
Last active September 12, 2024 15:40
filter_map VS map + compact
require 'benchmark/ips'
Benchmark.ips do |bm|
# Create a list with 10_000 elements and some nil values
list = (0..10_000).to_a
fn = ->(x) { x+1 if x.even? }
bm.report "select + map" do
list.select {|x| x.even? }.map {|x| x+1 }
@marcomd
marcomd / flat_map_compact_VS_filter_map_flatten.rb
Last active September 12, 2024 15:35
flat_map + compact VS filter_map + flatten
require 'benchmark/ips'
Benchmark.ips do |bm|
# Create a list with 10_000 elements and some nil values
list = (0..10_000).to_a
fn = ->(x) { [x, x * x] if x.even? }
bm.report "flat_map + compact" do
list.flat_map(&fn).compact
@marcomd
marcomd / sha256_md5_benchmark.rb
Created January 6, 2021 21:43
Benchmark ruby script MD5 vs SHA-256
require 'securerandom'
require 'digest'
require 'benchmark'
# Test weight
n = 1_000
started_time = Time.now
word_1k = SecureRandom.hex(512)
word_10k = SecureRandom.hex(5_120)
@marcomd
marcomd / rails_migration_create_countries.rb
Last active May 18, 2023 18:42
Rails migration to add countries table and populate it with countries gem
require 'countries/iso3166'
class CreateCountries < ActiveRecord::Migration[6.0]
def change
create_table :countries do |t|
t.string :name, limit: 64
t.string :name_it, limit: 64
t.integer :region, limit: 1
t.string :iso2, limit: 2
t.string :phone_prefix, limit: 3
@marcomd
marcomd / gist:3129118
Created July 17, 2012 12:18
Authenticate your API with devise, token by header
#Session controller provides a token
#/controllers/api/sessions_controller.rb
class Api::SessionsController < Devise::SessionsController
before_filter :authenticate_user!, :except => [:create]
before_filter :ensure_params_exist, :except => [:destroy]
respond_to :json
def create
resource = User.find_for_database_authentication(:email => params[:user_login][:email])
return invalid_login_attempt unless resource
@marcomd
marcomd / gist:1987631
Created March 6, 2012 17:28
Example 4
#yield
class A
def go(*attrs)
p "Before block"
attrs.each do |attr|
p yield(attr)
end if block_given?
p "After block"
end
end
@marcomd
marcomd / gist:1788853
Created February 10, 2012 11:27
ActiveAdmin: Add attributes_table to generic resource
#Add this new method
#activeadmin-0.4.0\lib\active_admin\views\pages\show.rb
#after def attributes_table
def attributes_table_for_resource(specified_resource, *args, &block)
panel(I18n.t('active_admin.details', :model => "#{specified_resource.class.name} #{specified_resource.id}")) do
attributes_table_for specified_resource, *args, &block
end
end
@marcomd
marcomd / devise.it.yml
Created August 4, 2011 14:39 — forked from jdeyla/devise.it.yml
Suitable for devise 1.4.2
it:
errors:
messages:
expired: "è scaduto ed è necessario richiederne uno nuovo"
not_found: "non trovato"
already_confirmed: "è già stato confermato, riprova a collegarti"
not_locked: "non era bloccato"
not_saved:
one: "un errore ha impedito il salvataggio di %{resource}:"
other: "%{count} errori hanno impedito il salvataggio di %{resource}:"