This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Kernel | |
TABLE_FLIP = <<-EOS | |
(╯°□°)╯︵ ┻━┻ | |
EOS | |
alias_method :original_puts, :puts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def set_context(c) | |
@context = c | |
end | |
def context | |
@context | |
end | |
def with_context(cont, &block) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SendMethodList | |
def send_method_list(*methods) | |
results = methods.map do |sym| | |
begin | |
self.send(sym) | |
rescue => error | |
error | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'securerandom' | |
REGEX = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])((?=.*[^\w])|(?=.*_))/ | |
def run | |
times = 0 | |
word = begin | |
times += 1 | |
SecureRandom.urlsafe_base64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def timer(seconds, &block) | |
Thread.new { | |
t_0 = Time.now | |
sleep seconds | |
elapsed = Time.now - t_0 | |
puts "timer triggered after #{elapsed} seconds (error: #{elapsed - seconds})." | |
yield | |
} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Foobar | |
module Config | |
class << self | |
attr_writer :foo, :bar, :baz | |
def foo(value = nil) | |
if value | |
self.foo = value | |
else | |
@foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
private | |
# Runs a rake task asyncronously in a child process | |
# | |
# for example, in any controller: | |
# async_rake("async:import_fixtures") | |
# async_rake("db:maintenance:cleanup", table: "things", ids: [12, 114, 539]) | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/md5' | |
class User | |
attr_accessor :name, :token_valid_for | |
attr_reader :pwd_hash | |
def initialize(name, pwd) | |
self.name = name | |
self.password = pwd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in a controller | |
# request.remote_ip -> http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-remote_ip | |
# default IP lookup -> http://api.rubyonrails.org/classes/ActionDispatch/RemoteIp/GetIp.html#method-i-calculate_ip | |
# | |
# request.remote_ip looks at different sources and makes a "best guess" to find the correct IP. This is usually easy | |
# with a plain app-server setup, but with reverse-proxy configurations (Mongrels behind Apache, Unicorns behind nginx) the | |
# requests are issued by the reverse-proxy (often localhost). | |
# | |
# Since we have a cluster of Unicorns behind a nginx reverse-proxy, the actual client IP is (almost) surely found in the |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# Author: Tommaso Pavese | |
# tommaso@pavese.me | |
# www.wonderingmachine.com | |
# | |
# Simple script for markdown using: | |
# | |
# Redcarpet: https://github.com/vmg/redcarpet | |
# Rouge: https://github.com/jayferd/rouge |