Skip to content

Instantly share code, notes, and snippets.

@tompave
tompave / closures.rb
Created December 16, 2015 00:52
closures in Ruby
i = 1000
f1 = Proc.new { |x| x * x }
f2 = -> (func, int) { puts "the result is: '#{func.call(int)}', i is: '#{i}'" }
def run(range, operation, printer)
range.each do |i|
printer.call(operation, i)
end
end
@tompave
tompave / rebooter.rb
Last active December 16, 2015 00:09
A simple implementation of a Ruby program that: works with a continuous event loop, is managed through UNIX signals, stops and reboots in response to file system changes (but that can be easily customized).
#! /usr/bin/env ruby
=begin
How to launch
$ chmod 700 rebooter.rb
$ ./rebooter.rb
It will create two files (will delete them on SIGTERM and SIGINT)
./rebooter.pid
./rebooter.control
@tompave
tompave / risotto.md
Created April 16, 2015 17:06
risotto!

Risotto for 4 people

Ingredients

  • rice (type: Carnaroli or Arborio)

    • 80-100 g per person, total: ~400 g
    • a bit more if you're hungry
  • one onion

@tompave
tompave / percent_encoding_guard.rb
Last active August 29, 2015 14:20
Rack middleware guards
# Issue on github:
# https://github.com/rack/rack/issues/337
#
# Possible patch:
# https://gist.github.com/psychocandy/3130349
# but I don't like the idea of monkeypatching the standard library
#
# Rack middleware:
# http://stackoverflow.com/questions/16269897/rails-argumenterror-invalid-encoding
#
@tompave
tompave / try_chain.rb
Created January 11, 2015 19:30
Try Chain
require 'active_support/core_ext/object/try'
module TryChain
def try_chain
@proxy = Proxy.new(self)
end
class Proxy
@tompave
tompave / impersonation_utilities.rb
Last active August 29, 2015 14:05
Poor Man's cross-authentication
# impersonation_utilities.rb
require 'digest/md5'
module ImpersonationUtilities
SHARED_SECRET = "I'm a secret string!"
TOKEN_VALID_FOR_DAYS = 1
class << self
@tompave
tompave / better_puts.rb
Created August 5, 2014 23:35
Improved, more sensible implementation of Ruby's Kernel#puts
module Kernel
TABLE_FLIP = <<-EOS
(╯°□°)╯︵ ┻━┻
EOS
alias_method :original_puts, :puts
@tompave
tompave / locals_and_blocks.rb
Last active August 29, 2015 14:04
Scoping in Ruby blocks
def set_context(c)
@context = c
end
def context
@context
end
def with_context(cont, &block)
module SendMethodList
def send_method_list(*methods)
results = methods.map do |sym|
begin
self.send(sym)
rescue => error
error
end
end
end
@tompave
tompave / random_iterations.rb
Created June 27, 2014 17:08
SecureRandom effectiveness
require 'securerandom'
REGEX = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])((?=.*[^\w])|(?=.*_))/
def run
times = 0
word = begin
times += 1
SecureRandom.urlsafe_base64