Skip to content

Instantly share code, notes, and snippets.

View stefanoc's full-sized avatar

Stefano Cobianchi stefanoc

View GitHub Profile
@stefanoc
stefanoc / protocol.rb
Last active August 29, 2015 14:04
Protocol implementation in Ruby
class Protocol
attr_reader :name, :ancestors
def initialize(name, ancestors, block)
@name = name
@ancestors = ancestors
@methods = []
instance_exec(&block)
end
@stefanoc
stefanoc / SassMeister-input.sass
Created September 25, 2014 15:22
Generated by SassMeister.com.
// ----
// Sass (v3.2.19)
// Compass (v0.12.6)
// ----
$price_classes: (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pc, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9)
$price_small: ((0px -122px, 20px, 42px, -5px), (-44px -122px, 13px, 42px, -7px), (-85px -122px, 14px, 42px, -6px), (-129px -122px, 20px, 42px, -6px), (-174px -122px, 20px, 42px, -11px), (-222px -122px, 18px, 42px, -8px), (-269px -122px, 19px, 42px, -9px), (-311px -122px, 16px, 42px, -12px), (-358px -122px, 14px, 42px, -7px), (-404px -122px, 15px, 42px, -6px), (-453px -127px, 1px, 42px, -1px), (-4px -179px, 9px, 42px, -8px), (-38px -179px, 9px, 42px, -11px), (-79px -179px, 8px, 42px, -8px), (-117px -179px, 10px, 42px, -10px), (-164px -179px, 9px, 42px, -8px), (-214px -179px, 6px, 42px, -6px), (-261px -179px, 6px, 42px, -6px), (-308px -179px, 4px, 42px, -5px), (-355px -179px, 7px, 42px, -6px), (-400px -179px, 7px, 42px, -5px))
$price_big: ((0px 0px, 30px, 61px, -5px), (-49px 0px, 32px, 61px, -7px), (-89px 0px, 44px, 61px, -7px), (-138px 0px, 40px, 61p
@stefanoc
stefanoc / test.rb
Created October 29, 2014 15:16
Word freqs
require 'pp'
dict = Hash.new(0)
File.open(ARGV[0], 'r') do |src|
while line = src.gets
line.strip.scan(/\w+/) { |word| dict[word] += 1 }
end
end
pp dict
@stefanoc
stefanoc / promise.rb
Created December 3, 2014 09:53
Promises
class Promise
class << self
alias :make :new
end
def initialize(block)
@result = []
@thread = Thread.new(@result) do |result|
result[0] = block.call
end
@stefanoc
stefanoc / Cargo.toml
Created February 5, 2015 20:14
Rust word counter
[package]
name = "wc"
version = "0.0.1"
authors = ["stefano"]
[dependencies]
regex = "0.1.12"
regex_macros = "0.1.6"
@stefanoc
stefanoc / computed_attributes.rb
Created February 17, 2015 21:55
Computed attributes
module ComputedAttributes
module ClassMethods
def compute(name, components, &block)
getter = -> do
value = instance_variable_get("@_#{name}")
unless value
value = instance_exec(&block)
instance_variable_set("@_#{name}", value)
end
value
@stefanoc
stefanoc / dice.rb
Last active August 29, 2015 14:15
Dadi
require 'pp'
dice = [0, 0, 1, 2, 3, 4]
throws = dice.product(dice, dice, dice, dice)
counts = throws.inject(Hash.new(0)) { |t, p| t[p.inject(&:+)] += 1; t }
pp counts.map { |(k, v)| [k, (v.fdiv(throws.length) * 100).round(3)] }
@stefanoc
stefanoc / tis100.rs
Created August 21, 2015 22:31
tis-100
#![allow(dead_code)]
use std::thread;
use std::sync::mpsc::{sync_channel, SyncSender, Receiver};
use std::collections::HashMap;
struct Port {
rx: Receiver<i32>,
tx: SyncSender<i32>
}
@stefanoc
stefanoc / xss_sanitizer.b
Created August 25, 2015 12:38
XSS sanitizer
module XssSanitizer
extend ActiveSupport::Concern
class Scrubber < Loofah::Scrubber
ALLOWED_TAGS = %w(
strong span em b u i a
h1 h2 h3
div p ul ol li blockquote br
)
require "thread"
module Synchronizable
def self.included(base)
base.send(:include, InstanceMethods)
base.send(:extend, ClassMethods)
end
module ClassMethods
def synchronized(meth)