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
@cjus
cjus / jsonval.sh
Created June 26, 2011 17:42
Extract a JSON value from a BASH script
#!/bin/bash
function jsonval {
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $prop`
echo ${temp##*|}
}
json=`curl -s -X GET http://twitter.com/users/show/$1.json`
prop='profile_image_url'
picurl=`jsonval`
@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) {
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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)