Skip to content

Instantly share code, notes, and snippets.

@apeiros
apeiros / method_cop.rb
Created October 31, 2011 23:06 — forked from babney/method_cop.rb
MethodCop
module MethodCop
# guard methods that have side effects with a callback that fires before the method is invoked. If the callback returns a "falsey" value,
# the method is halted and will not be called. The callback will return nil instead.
# if the method does not have side effects or you depend on its return value, you should NOT use this on that method!
def guard_method(guarded_method, guard=nil, &callback)
# normalize guard
guard = method(guard) if guard.is_a?(Symbol)
guard = callback if callback
raise ArgumentError, "You can only supply either a guard argument or a block" if block && guard
@apeiros
apeiros / wav.rb
Created November 6, 2011 19:10
Fun with pure tones and WAV (see code after __END__)
class Numeric; def cap(min,max) return min if self < min; return max if self > max; self; end; end
class Wav
module Generators
end
def self.mnot(str)
tones = {
'C' => 264.0,
@apeiros
apeiros / proxymanager.rb
Created November 13, 2011 00:29
proxymanager.rb by jake232 - refactoring
class ProxyManager
class Proxy
attr_reader :address
attr_accessor :last_used
def initialize(address, last_used=nil)
@address = address
@last_used = last_used
end
@apeiros
apeiros / progressbar.rb
Created January 28, 2012 09:18
A progressbar in the command line
# Usage
# bar = ProgressBar.new
# bar.init
# 0.step(100, 0.5) { |i|
# bar.update(i)
# sleep(rand*0.1)
# }
# bar.finish
#
# you can update the progress and render independently if you wish. Use
@apeiros
apeiros / https_echo.rb
Created January 29, 2012 19:21
HTTPS echo server, using only OpenSSL and TCPServer
# use the following commands to create key.pem & cert.pem
# openssl genrsa -out key.pem
# openssl req -new -x509 -key key.pem -out cert.pem -days 1095
# open your browser and use
# https://127.0.0.1:8080/
require 'socket'
require 'openssl'
require 'cgi'
@apeiros
apeiros / gist:1770565
Created February 8, 2012 15:48
Execute a command via ssh and get stdout, stderr and exitstatus returned
class Net::SSH::Connection::Session
def exec3!(command, env=nil)
stdout, stderr, exitstatus = "", "", nil
open_channel do |channel|
if env then
env.each do |name, value|
channel.env(name, value)
end
end
channel.exec(command) do |ch, success|
@apeiros
apeiros / gist:1791790
Created February 10, 2012 19:03
Enumerable#last, halfway scalable
module Enumerable
unless method_defined?(:last) then
def last(n=nil)
reverse_each_method = method(:reverse_each)
has_reverse_each = reverse_each_method && reverse_each_method.owner != Enumerable # native reverse_each needed
if n then
return_value = []
if has_reverse_each then
reverse_each { |val|
return_value.unshift(val)
@apeiros
apeiros / remplate.rb
Created March 19, 2012 14:13
Controller-less rails template
class Remplate < AbstractController::Base
include AbstractController::Rendering
include AbstractController::Layouts
include AbstractController::Helpers
include AbstractController::Translation
include AbstractController::AssetPaths
include Rails.application.routes.url_helpers
self.view_paths = "app/views" # TODO: get it from Rails.application if available
@apeiros
apeiros / inheritable_attributes.rb
Created June 19, 2012 17:15
Inheritable class level instance variables
module InheritableAttributes
def self.extended(klass)
klass.instance_variable_set(:@inheritable_attributes, [])
end
attr_reader :inheritable_attributes
def inheritable_attribute(*args)
@inheritable_attributes.concat(args)
singleton_class.send :attr_accessor, *args unless args.empty?
@apeiros
apeiros / capabilities.rb
Created June 29, 2012 22:38
Require files and provide an interface to check what's available
Moved to https://github.com/apeiros/available