Skip to content

Instantly share code, notes, and snippets.

@eric1234
eric1234 / csv_reader_with_header.rb
Created September 10, 2009 20:08
CSV::Reader extension: Utilize header row
# Extends CSV::Reader to assume the first row is the headers to the data
# (i.e. fields names). Each non-header row in the data with then be
# passed a hash instead of the normal array so you can reference the
# data by field name. For example:
#
# CSV::Reader::WithHeader.parse(io) do |row|
# puts row['first_name'] + ' ' + row['last_name']
# end
class CSV::Reader::WithHeader < CSV::Reader
def self.parse(str_or_readable, fs=',', &blk)
@eric1234
eric1234 / import_helper.rb
Created September 10, 2009 21:30
Simple method to help import a data while normalizing at the same time.
# Most simply just returns the data. But can allow for adjusting of the
# data a bit more to ensure more normalized data or format conversions.
#
# * If data is blank? (as defined by ActiveSupport) then nil is
# returned.
# * If the block is given then that will be called and the return
# value will be passed back. If the value is blank? then the block
# will not be called and nil will be returned (to avoid checks
# in the blocks)
#
@eric1234
eric1234 / podcast_slurp.rb
Created September 11, 2009 18:44
Will slurp media attached to audio/video podcasts.
#!/usr/bin/env ruby
# Will slurp all the media attached to a audio/video podcasts. USAGE:
#
# ruby -rrubygems podcast_slurp.rb http://feeds.feedburner.com/railscasts railscasts
$VERBOSE = true
require 'open-uri'
require 'nokogiri'
# An example of verbose Ruby code to counter the argument that
# optional syntax should be used as expressed on the blog post
# http://robots.thoughtbot.com/post/185504560/to-self-or-not-to-self
#
# I.E. the logical conclusion of this sort of thinking.
class User < ActiveRecord::Base
self.before_save(:sanitize_names);
def display_name()
return self.email() if self.first_name().blank?();
@eric1234
eric1234 / git-hub-fork-network.rb
Created September 14, 2009 18:33
Will list all forks related to a project on GitHub.
#!/usr/bin/env ruby
# Provides a way to list forks on github for a project.
#
# git-hub-fork-network user project
require 'rubygems'
require 'octopi'
module Octopi
class Repository < Base
@eric1234
eric1234 / gist:186833
Created September 14, 2009 18:35
Will compare a fork of a project with the original to get a single diff of what the fork has done.
#!/bin/sh
# Provides a way to compare a fork against the original it is based
# on so you can get a general idea of what is in a fork.
#
# git-fork-compare <original> <fork>
TMP_GIT=`mktemp -d`;
rmdir $TMP_GIT;
git clone -q $2 $TMP_GIT;
cd $TMP_GIT;
@eric1234
eric1234 / legacy_csv_data_import_template.rb
Created September 14, 2009 18:52
Script Template: Import legacy CSV data into Rails 1.x, 2.x app
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require File.dirname(__FILE__) + '/../config/environment'
require 'csv'
# http://gist.github.com/184784
class CSV::Reader::WithHeader < CSV::Reader
def self.parse(str_or_readable, fs=',', &blk)
headers = nil
super str_or_readable, fs do |row|
@eric1234
eric1234 / p1.rb
Created September 19, 2009 10:35
Solution to problem 1 on Project Euler
class Numeric
def divisible_by? num
self % num == 0
end
end
module Enumerable
def sum &blk
blk = proc {|x| x} unless block_given?
inject(0) {|sum, element| sum + (blk[element] || 0)}
@eric1234
eric1234 / p2.rb
Created September 19, 2009 11:16
Problem 2 on Project Euler
class Numeric
def even?
self % 2 == 0
end
end
module Enumerable
def sum &blk
blk = proc {|x| x} unless block_given?
inject(0) {|sum, element| sum + (blk[element] || 0)}
@eric1234
eric1234 / enum_match.rb
Created September 21, 2009 17:47
Using the match operator as an alternative to include?
class Object
def =~ n
return n =~ self if n.is_a?(Enumerable)
false
end
end
module Enumerable
alias_method :=~, :include?
end