Skip to content

Instantly share code, notes, and snippets.

View mharris717's full-sized avatar

Mike Harris mharris717

View GitHub Profile
require 'rubygems'
require 'mharris_ext'
require 'mongo'
class OrderedHash
def reject(&b)
res = OrderedHash.new
each { |k,v| res[k] = v unless yield(k,v) }
res
end
class Mongo::Collection
def sum_by_raw(ops)
reduce_function = "function (obj, prev) { prev.count += (obj.#{ops[:sum_field]} ? obj.#{ops[:sum_field]} : 0); }"
code = Mongo::Code.new(reduce_function)
group([ops[:key]].flatten, ops[:filter]||{}, {"count" => 0},code)
end
def sum_by(ops)
sum_by_raw(ops).inject({}) { |h,a| k = ops[:key]; h.merge(a[k] => a['count'])}
end
end
class Client < ActiveRecord::Base
validates_presence_of :name, :zip, :address, :city, :phone_number
# only change the humanized text for zip and name. The other attributes will still be humanized
# just as they normally would.
HUMANIZED_COLUMNS = {:zip => "Postal code", :name => "Company contact name"}
def self.human_attribute_name(attribute)
HUMANIZED_COLUMNS[attribute.to_sym] || super
end
#include <iostream>
using namespace std;
int leadingOnes(int num) {
for(int i=0;i<8;i++) {
if (num >= 128) {
num = (num << 1) & 255;
}
else {
return i;
# Putting the method on Array, instead of Enumerable, because I don't want hashes to flatten.
# There are other "flattenable" structures aside from an Array, but I am ignoring that.
class Array
# A structure is defined as "flattenable" if it responds to my_flatten
def my_flatten
res = []
each do |obj|
if obj.respond_to?(:my_flatten)
res.concat(obj.my_flatten)
# Lets you define relations between ActiveRecord and Mongo objects
module MongoRelations
module InstanceMethods
# get an object representing the MongoMapper equivalent of this object
# for the purpose of access relations on the object
fattr(:mongo_obj) do
res = klass.mongo_class.new
my_id = id
res.class_eval do
# gem install nokogiri before running for first time
require 'nokogiri'
require 'open-uri'
def sportsbook_line
doc = Nokogiri::XML(open("http://americasline.com/"))
doc.css("#chalk tr[3] td[5]").text
end
@mharris717
mharris717 / copy_to_subdirs.rb
Created October 20, 2011 16:18
Copy file to subdirs
task :copy_to_subdirs do
### CHANGE THESE TWO VARIABLES
### USE FORWARD SLASHES IN THE DIRECTORY NAMES
directory_with_files_to_copy = "c:/code/test_dir9"
dir_containing_subdirs = "c:/code/test_dir9/some_dir"
source_files = Dir["#{directory_with_files_to_copy}/*"].select { |x| FileTest.file?(x) }
subdirs = Dir["#{dir_containing_subdirs}/*"].select { |x| FileTest.directory?(x) }
@mharris717
mharris717 / files_in_dir_of_zips.rb
Created March 20, 2012 18:35
Get files in dir of zips
require 'zip/zipfilesystem'
require 'net/ftp'
require 'csv'
def files_in_zip(filename)
Zip::ZipFile.open(filename) do |zip|
res = []
dirs = [""]
while dirs.size > 0
d = dirs.pop
def timelist
times = contents_by_selector("SBR.htm",".eventLine-time")
times.collect! { |x| [x,x] }
times.flatten!
return times
end
def timelist
times = contents_by_selector("SBR.htm",".eventLine-time")
times.collect! { |x| [x,x] }