Skip to content

Instantly share code, notes, and snippets.

View fabioyamate's full-sized avatar

Fabio Yamate fabioyamate

View GitHub Profile
@fabioyamate
fabioyamate / polling.js
Last active December 25, 2015 20:09
polling using jQuery deferred with timeout, similar to go channels
/* Performs a pooling with a timeout limit and throttle frequency.
fn - a function that returns a promise of an async computation
timeout - time in milliseconds to cancel pooling (default 10s)
throttle - the minimumm time between pollings
Use throttle if the async computation is too fast and you want
that occurrances happens in intervals.
*/
var polling = function(fn, timeout, throttle) {
@fabioyamate
fabioyamate / type_checking.rb
Created October 11, 2013 01:04
type checking
# gem install mar
# this only checks at runtime, so it doesn't verify the type at compilation
require 'mar'
def type_check(type, value)
value.is_a?(type) or raise "#{value} is not type of #{type}"
end
def type_signature(*types)
var quotes = [{ symbol: 'MSFT', price: 27.01 },
{ symbol: 'INTC', price: 21.75 },
{ symbol: 'MSFT', price: 27.96 },
{ symbol: 'MSFT', price: 31.21 },
{ symbol: 'INTC', price: 22.54 },
{ symbol: 'INTC', price: 20.98 },
{ symbol: 'MSFT', price: 30.73 }]
function priceIncrease(w) {
return w[1].price / w[0].price - 1;
#!/bin/bash
# Run the script as root user
#
# copy the script to the /root and chmod +x to the file.
#
# $ ./rbenv-install-system-wide.sh
#
# and wait
#!/usr/bin/env ruby
require 'socket'
test_file = ARGV[0]
socket = UNIXSocket.new('testing.sock')
socket.write(test_file)
socket.close_write
@fabioyamate
fabioyamate / reduce.rb
Created June 26, 2013 04:23
reduce application
# the common thinking when you are transforming some data to a non-nil value is use map
a = { foo: 'bar', bar: 'baz' }
[:foo, :bar, :baz, :foobar].map { |k| a[k] }
# => ['bar', 'baz', nil, nil]
# to drop nil cases
[:foo, :bar, :baz, :foobar].map { |k| a[k] }.compact
# => ['bar', 'baz']
@fabioyamate
fabioyamate / iframe.html
Last active December 18, 2015 19:19
postMessage
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>postMessage</title>
</head>
<body>
<div id="container">
iFrame
<button>post message</button>
@fabioyamate
fabioyamate / resque-retry.rb
Last active December 18, 2015 18:09
resque failures
describe "job failures" do
it 'retries with limited attempts' do
require 'resque/failure/redis'
Resque.unstub(:enqueue)
Resque::Failure::MultipleWithRetrySuppression.classes = [Resque::Failure::Redis]
Resque::Failure.backend = Resque::Failure::MultipleWithRetrySuppression
JobWithError = Class.new do
@queue = "test"
.inline-block {
background: red;
margin-right: -4px;
}
.with-sidenote:after {
white-space: pre;
}
@fabioyamate
fabioyamate / dabblet.css
Created April 27, 2013 02:24
Circular Tooltip (SO)
/**
* Circular Tooltip (SO)
* http://stackoverflow.com/q/13132864/1397351
*/
* { margin: 0; padding: 0; }
body {
overflow: hidden;
background: url(http://theearlcarlson.com/experiments/amTooltip/img/bg.jpg);
}
/* generic styles for button & circular menu */