This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] } |
OlderNewer