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
# 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) |
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
# 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) | |
# |
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
#!/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' |
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
# 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?(); |
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
#!/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 |
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
#!/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; |
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
#!/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| |
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 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)} |
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 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)} |
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 Object | |
def =~ n | |
return n =~ self if n.is_a?(Enumerable) | |
false | |
end | |
end | |
module Enumerable | |
alias_method :=~, :include? | |
end |
OlderNewer