Skip to content

Instantly share code, notes, and snippets.

# An extension to the Hash class to inspect what it is made of
class Hash
def whatami
keys.inject({}) do |hash, key|
hash[key] = case self[key].class.to_s
when "Hash"
self[key].whatami
when "Array"
self[key].map do |item|
# Simple extension to string to cut out the middle and leave the beginning and end
class String
def middle_truncate(n, options={})
return self if self.length <= n
elipses = options.delete(:elipses) || 3
side = (n/2 - elipses/2)
left_side = (elipses % 2 == 0) ? side : side+1
left, right = self[0..left_side], self[(self.length-side+elipses)..self.length]
elipses_text = "." * elipses
return "#{left}#{elipses_text}#{right}"
require "rubygems"
require "crack"
xml = <<-XML
<parent_node>
<content>
<item attr1="test">this is a string that ruins my life</item>
<item attr1="i am an attribute and i live nicely in the world of hashes" />
</content>
</parent_node>
XML
#!/bin/bash
echo "Setting up chef..."
sudo apt-get -y update
sudo apt-get -y install ruby ruby1.8-dev libopenssl-ruby1.8 rdoc ri irb build-essential wget ssl-cert
cd /tmp
wget http://rubyforge.org/frs/download.php/57643/rubygems-1.3.4.tgz
tar zxf rubygems-1.3.4.tgz
User.find(:all, :conditions => "created_at BETWEEN '2004-01-01 12:00:00' AND 2005-01-01 12:00:00")
def pph(hash, depth=0, output="")
if hash.is_a?(Hash)
carriage_return = depth > 0 ? "\n" : ""
output << carriage_return
hash.each do |key, val|
output << "\t" * depth + "#{key}\t=>\t#{pph(val, depth+1)}"
end
elsif hash.is_a?(Array)
return "["+hash.join(", ")+"]" +"\n"
else
#!/usr/bin/env ruby
# Author => Jason Amster
# Email => [email protected]
# Time.now => Thu Jul 16 17:33:50 -0400 2009
# Purpose => To get those friggin error codes off of Authorize.net and into somthing i can actually use
require 'rubygems'
require "activesupport"
require 'hpricot'
require 'open-uri'
#
# bash completion support for core Git.
#
# Copyright (C) 2006,2007 Shawn O. Pearce <[email protected]>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.
#
# The contained completion routines provide support for completing:
#
# *) local and remote branch names
while 1
unixtime = Time.new.strftime('%s')
if Integer(unixtime) > 1234567890
puts "HOORAAAYYYY Unix Time > 1234567890 - #{unixtime}"
else
puts "Booooo Unix Time not yet #{unixtime}"
end
sleep 1
end
# EXTRACTED FROM THIS THREAD:
# http://rails.lighthouseapp.com/projects/8994/tickets/879-finding-the-days-weeks-months-years-between-two-dates
require "rubygems"
require "activesupport"
#activesupport is necessary for Date.parse (I think)
class Date
def self.days_between(start, finish)
(finish - start).to_i
end