Skip to content

Instantly share code, notes, and snippets.

View huned's full-sized avatar
💭
I may be slow to respond.

Huned Botee huned

💭
I may be slow to respond.
View GitHub Profile
@huned
huned / gist:89c44bcfd76f1add3b31
Created June 3, 2015 17:12
`inject(:+)` vs `each +=`
irb(main):003:0> require 'benchmark'
=> true
irb(main):004:0> numbers = nil
=> nil
irb(main):005:0> puts Benchmark.measure { numbers = 1_000_000.times.map {|i| i.to_f }; 1 }
1.110000 0.000000 1.110000 ( 0.448476)
=> nil
irb(main):006:0> numbers.length
=> 1000000
irb(main):007:0> puts Benchmark.measure { numbers.inject(:+) }
2010-01-12
2010-01-13
2010-01-14
2010-01-15
2010-01-16
2010-01-19
2010-01-20
2010-01-21
2010-01-22
2010-01-23
def rescue_retry(tries = 2, options = {}, &block)
raise ArgumentError, 'gimme a block' unless block_given?
begin
tries -= 1
yield
rescue => e
if tries > 0
sleep(options[:sleep]) if options[:sleep]
retry
#!/usr/bin/env ruby
File.readlines('./tmp/5.log').each.with_index do |line, i|
matches = line.match(/\s+MOPED: \S+ (\w+)/i)
next if matches.nil?
op = matches.to_a.last.downcase
t = line.match(/\(([.0-9]+)ms\)$/i).to_a.last
puts "#{i},#{op},#{t}"
end
@huned
huned / complete_nary_tree.rb
Last active August 29, 2015 14:15
Generates a Complete N-ary Tree given parameters (height, degree) in dot format. Visualize with graphviz.
#!/usr/bin/env ruby
class CompleteNaryTreeGenerator
def self.generate_dot(height, degree)
puts 'digraph {'
# Convenience.
d = degree
h = height
i = 1
@huned
huned / random
Last active August 29, 2015 14:15
#!/bin/sh
# Generate n random numbers in [0, 999]. n defaults to 100 but override it with $1.
if [ "$1" == '' ]; then
upto=100
else
upto=$1
fi
for i in `seq 1 $upto`; do echo $(($RANDOM % 1000)); done
#!/bin/sh
# Plot an ASCII histogram of numbers from stdin. Uses gnuplot.
# Optionally specify bin size as an argument.
if [ "$1" == '' ]; then
binsize=100
else
binsize=$1
fi
@huned
huned / id_dsa.pub
Last active August 29, 2015 14:10
huned@omg's public key
ssh-dss AAAAB3NzaC1kc3MAAACBAIE9R4sFArK2lrn0l0MLRFxn0ExUMIH/4VPg2gQxE9a1KwTr9nbQpL6H+mxxZLNm9+iKZeKrx+PEqNH6SmhYx0LUQ7FqaYhy89Leoy5uUW87CSd9x7KuxBChK9vvqyf8X7b2mGzWgs5XRzSiLHngn8XGHxVgXQoS1K/w7M+c4I2nAAAAFQDEhsSJQOzio1v8Jiy5ypALwxnaswAAAIB7STNNhWdkb7SeHs0zEEkwgs0nExzBFAZ3WAmZZnb8m9HWTLbwZpGGPRfcWi0cthHIwfzATvzUwEZuzjmeqhS9J7UxSaPOpzg4mTSQFvj6ZHzTg+ZwhNIRAOOaXV2lopq9KChV+ZSjtmZQDs6Js0oJEAuw5DCktKTnkZ1Kf8hsfQAAAIBattEuuohpFnTY5VM8B7mMVib1y1jQ0yPa+sORBFqzdGeJxoPZJvGXhO5L90KW20VtLGRbrrkdrZ75/7Cy1mISe6+Of7WBaZniL+kxUN4+hwuw/2aWjgOUstEhFOf3rilinMc/wBtCjpe5GLLEbDTGLAlZbS0/GwS+hh1GLKfdmg== huned@omg
@huned
huned / activeresource_base_callable_user.rb
Created September 5, 2014 20:57
Monkey patch ActiveResource::Base.user so that it calls @user if user is callable, otherwise uses the default behavior.
module ActiveResource
class Base
class << self
# Monkey patch ActiveResource::Base.user a little. We want it to call
# @user if @user is callable, otherwise use the default behavior.
def user_with_lambda
if defined?(@user) && @user.respond_to?(:call)
@user.call(self)
else
user_without_lambda # default behavior
require File.expand_path('../../config/environment', __FILE__)
require 'csv'
intermediaries = Person.intermediary.asc(:name).to_a
intermediaries.each do |person|
company = person.current_company
latest_activity =
if company.present?
company.activities.desc(:created_at).where(:kind.in => Activity::FOLLOWUPABLE_KINDS).first
else