Skip to content

Instantly share code, notes, and snippets.

View mssola's full-sized avatar
🏠
Working from home

Miquel Sabaté Solà mssola

🏠
Working from home
View GitHub Profile
g6Rib2R5hqhkZXRhY2hlZMOpaGFzaF90eXBlCqNrZXnEIwEgG3AOLgwYvl6HQqL7124aTV4apRoR5vP1Vd/S1PASk0UKp3BheWxvYWTFAuh7ImJvZHkiOnsia2V5Ijp7ImVsZGVzdF9raWQiOiIwMTIwYzJlYTViODFkNjYxMjNkMGI3NDIzNWNlNjZmY2RlZGFmZjRkYTFmODg0MjZhMmQ3MjkyY2FhMTdhOGZjNzkxNDBhIiwiaG9zdCI6ImtleWJhc2UuaW8iLCJraWQiOiIwMTIwMWI3MDBlMmUwYzE4YmU1ZTg3NDJhMmZiZDc2ZTFhNGQ1ZTFhYTUxYTExZTZmM2Y1NTVkZmQyZDRmMDEyOTM0NTBhIiwidWlkIjoiZTI0MDU5N2UwNzI3ZmE4MzVhOGViOWE3N2QyODBmMTkiLCJ1c2VybmFtZSI6Im1zc29sYSJ9LCJzZXJ2aWNlIjp7Im5hbWUiOiJnaXRodWIiLCJ1c2VybmFtZSI6Im1zc29sYSJ9LCJ0eXBlIjoid2ViX3NlcnZpY2VfYmluZGluZyIsInZlcnNpb24iOjF9LCJjbGllbnQiOnsibmFtZSI6ImtleWJhc2UuaW8gZ28gY2xpZW50IiwidmVyc2lvbiI6IjEuMC4xNSJ9LCJjdGltZSI6MTQ1OTUwNTc5MCwiZXhwaXJlX2luIjo1MDQ1NzYwMDAsIm1lcmtsZV9yb290Ijp7ImN0aW1lIjoxNDU5NTA1Nzc3LCJoYXNoIjoiZTVkZmQxNmNhMmNiYjY3ZTA1MTk1MGIyNGFhNzI2OTVhOGJmNjA5MDU2OTA4NWY4Yjg5NDdkZWNmMGVlY2FiOTA1OWY3MzcyYjQ2ZmMyYTJmYTFiYWFiZDQ2MzE5YjFmMDlhNzg0MDY4YjBmOWZiOGFhM2Q0YzFlMzc1YzBmNTUiLCJzZXFubyI6NDI2NDU1fSwicHJldiI6ImEyOTU5MmQ2N2RhMDRiNmNjYzQxZDFlYzcwNzQ5ZTBmYzJmZmI2
@mssola
mssola / model.py
Created October 23, 2013 12:35
Map model names into database tables by using metaprogramming in Python.
class Persistent(type):
def __new__(self, name, bases, dct):
self.tables = dict()
return super(Persistent, self).__new__(self, name, bases, dct)
def __init__(self, name, bases, dct):
table = name.lower() + 's'
self.tables[self] = table
super(Persistent, self).__init__(name, bases, dct)
@mssola
mssola / virtual.rb
Created August 2, 2013 07:46
Simple and stupid code to emulate pure virtual methods in Ruby.
##
# This file describes a simple way to implement the idea of pure virtual
# methods (abstract methods in Java, C#,...) in Ruby. Personally, I haven't
# used this pattern while programming in Ruby, but I thought it could be fun.
#
##
# This class provides a fancy way to show an error when a virtual method
# is being used but the subclass hasn't implemented it.
@mssola
mssola / socket.rb
Created August 2, 2013 07:45
Testing HTTP's Transfer-Encoding: chuncked.
#
# This is a simple script that creates a TCP server and then transfers
# the info via streaming. It's not too fancy, I'm just testing the HTTP's
# Transfer-Encoding: chunked;
#
require 'socket'
# This is the message to be sent.
@mssola
mssola / singleton.rb
Created August 2, 2013 07:44
Different ways to create the Singleton pattern in Ruby.
##
# This files shows some possible implementations of the Singleton pattern
# in Ruby. I'm not a huge fan of the Singleton pattern, but it's nice
# in some cases. In this file I'm going to implement a simple logger.
#
##
# The first implementation that can come to our minds is to create a class
# that holds an instance as a class variable that can be accessed through
@mssola
mssola / rom.rb
Created August 2, 2013 07:41
A simple gist showing as much of the Ruby Object Model as possible.
##
# This file describes some topics regarding the Ruby Object Model.
#
require 'pp'
##
# First of all, let's take a look at the very basics.
module Modul
@mssola
mssola / flip_flop.rb
Created August 2, 2013 07:39
Because Ruby can flip and flop :P
# This file shows a somewhat rare feature of Ruby with the .. operator.
# It's a a perlism called "flip-flop". This feature will make it into Ruby 2.0
# but its future is uncertain (probably removed in a future Ruby 3.0 ?).
#
# More on flip-flops here: https://bugs.ruby-lang.org/issues/5400
# Prints all the lines between begin..end in the DATA section.
DATA.each_line do |line|
@mssola
mssola / closures.pl
Created August 1, 2013 21:29
Playing with decorators and closures in Perl. Nothing fancy, just a quick reference.
#!/usr/bin/perl
use strict;
# Decorator. Same idea as the Python decorators. In this case it takes a
# function as the only argument and returns a closure that returns the result
# of the originally passed function plus two.
sub plus_two
{
my ($f_ref) = @_;
@mssola
mssola / references.pl
Created August 1, 2013 20:36
Simple but quite complete example of references in Perl.
#!/usr/bin/perl
use strict;
##
# List & hash references.
package Class
{
@mssola
mssola / consumer_producer.go
Last active December 18, 2015 20:29
A simple solution to the classic problem of the "producer-consumer" in Go.
package main
import (
"fmt"
"math/rand"
)
const N_WORKERS = 10
func producer(c chan int) {