Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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
class Module
def swap_method(a, b)
x = instance_method(a)
y = instance_method(b)
define_method(b, x)
define_method(a, y)
end
end
@apeiros
apeiros / literalparser.rb
Created July 22, 2011 20:13
Parse literals to ruby objects
require 'strscan'
require 'bigdecimal'
# This is copied and slightly refactored from BareTest::TabularData
#
# Example
# LiteralParser.parse("nil") # => nil
# LiteralParser.parse(":foo") # => :foo
@apeiros
apeiros / dirtyhash.rb
Created July 8, 2011 19:27
A hash preserving changes made to it
require 'hash/dirty'
# DirtyHash behaves just like Hash, but keeps track of changes applied to it.
#
# @example
# dh = DirtyHash.with :x => 1
# dh.clean? # => true
# dh.update :y => 2, :z => 4
# dh.changed # => {:y => [:added, 2], :z => [:added, nil, 4]}
# dh[:z] = 3
@apeiros
apeiros / seeds.rb
Created May 26, 2011 17:22
A beefed up seeds file for rails
require 'pp'
puts "Seeding for env '#{Rails.env}'"
# disable AR logger
old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil unless $VERBOSE
env_seed_file = "#{Rails.root}/db/data/seed/#{Rails.env.downcase}/seeds.rb"
# first load yaml files that is "base" loading