Skip to content

Instantly share code, notes, and snippets.

@andruby
andruby / _introspection.rb
Created July 15, 2011 12:18
method introspection in Ruby 1.9.2
include Test::Unit::Assertions
#=> Object
m = method(:assert_in_delta)
#=> #<Method: Object(MiniTest::Assertions)#assert_in_delta>
m.methods - Object.methods
#=> [:call, :[], :arity, :to_proc, :receiver, :owner, :unbind, :source_location, :parameters]
m.parameters
#=> [[:req, :exp], [:req, :act], [:opt, :delta], [:opt, :msg]]
m.arity
#=> -3
@andruby
andruby / gist:1024707
Created June 14, 2011 11:18
Ruby 1.9.0 group_by benchmark
require 'rubygems'
require 'activesupport'
require 'benchmark'
include Benchmark
class T
attr_accessor :date, :count
def initialize(_date,_count)
@count = _count
@date = _date
@andruby
andruby / gist:1012748
Created June 7, 2011 17:53
BERT-RPC / Ernie bug
BERTRPC::Service.new("localhost", 9999).call.ernie.echo(2282389)
@andruby
andruby / gist:992773
Created May 26, 2011 08:28
Find the first 1000 primes quickly in Ruby
getal = 2
primes = [2]
begin
getal += 1
primes << getal unless primes.any? { |deler| getal % deler == 0 }
end until primes.size >= 1000
p primes
@andruby
andruby / deploy.rb
Created January 26, 2011 19:48
Start and Stop tasks for resque workers, with capistrano deploy hook (without God)
after "deploy:symlink", "deploy:restart_workers"
##
# Rake helper task.
# http://pastie.org/255489
# http://geminstallthat.wordpress.com/2008/01/27/rake-tasks-through-capistrano/
# http://ananelson.com/said/on/2007/12/30/remote-rake-tasks-with-capistrano/
def run_remote_rake(rake_cmd)
rake_args = ENV['RAKE_ARGS'].to_s.split(',')
cmd = "cd #{fetch(:latest_release)} && #{fetch(:rake, "rake")} RAILS_ENV=#{fetch(:rails_env, "production")} #{rake_cmd}"
# Application Generator Template
# Modifies a Rails app to use Mongoid, Devise, jQuery, Haml
# Usage: rails new app_name -m http://gist.github.com/raw/452364/gistfile1.txt
# More info: http://github.com/fortuity/rails3-mongoid-devise/
# If you are customizing this template, you can use any methods provided by Thor::Actions
# http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor/Actions.html
# and Rails::Generators::Actions
# http://github.com/rails/rails/blob/master/railties/lib/rails/generators/actions.rb
@andruby
andruby / problem.rb
Created May 8, 2010 23:27
My solution to Google's CodeJam 2010
# Helper class for file I/O and time measurement
class Problem
def initialize(input_file,output_file=nil)
@output_file = output_file
@start_time = Time.now
@input_file = input_file
@output_file ||= input_file.gsub('.txt','').gsub('.in','.out')
File.delete(@output_file) if File.exists?(@output_file)
end
# Script to setup swap space on a Amazon AWS instance
# Takes about 1 minute per GB of swap size
# run it like this:
# ruby aws_swap.rb 4G
SWAP_LOCATION = "/mnt/swapfile"
def run(cmd)
if ARGV.join(' ').include?('-p')
puts cmd
@andruby
andruby / dyndns_update.rb
Created April 17, 2010 11:01
Zerigo dynamic dns updater
#!/usr/bin/env ruby
# Dynamic DNS updater by Andruby for Zerigo
# www.andrewsblog.org
ApiKey = 'myzerigoapikey' # your Zerigo API key
Host = 'test.example.com' # the host you want to dynamically update
User = '[email protected]' # your Zerigo username
NameServer = 'a.ns.zerigo.net' # Zerigo nameserver to query
LastIpTmpFile = '/tmp/dyn_update_last_ip' # a temporary file where we store the last ip adress
@andruby
andruby / ps_avg.rb
Created January 23, 2010 17:17
Sample the CPU Load and Memory usage of an application over time and returns the average, standard deviation and min-max range.
# Sample the CPU Load and Memory usage of an application over time and
# returns the average, standard deviation and min-max range.
# processname to check as $1, number of samples as $2
# eg: ruby ps_avg.rb safari 100
process_name = ARGV[0]
sample_count = ARGV[1] || 120
samples_per_seconds = 3
sleep_time = (1/samples_per_seconds.to_f)