Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env bash
mate .autotest app/ features/ config/ lib/ db/ vendor/gems/ vendor/plugins/ public/ test/ spec/ Rakefile README;
class String
def to_name
firstname,middlename,lastname = nil,nil,nil
name = self
name_ary = name.split(" ")
if name_ary.size > 2
firstname,middlename,lastname = name_ary[0],name_ary[1],name_ary[2..name_ary.size].join(" ")
else
firstname,lastname = name_ary[0],name_ary[1]
end
def quicktime_movie(url, options = {})
filename = url.scan(/\w+\.mov$/)
url.gsub!(filename)
object_tag_options = {
:CLASSID => "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B",
:width => "320",
:height => "256",
:codebase => "http://www.apple.com/qtactivex/qtplugin.cab"
}.update(options)
require 'rubygems'
begin
gem 'gem_name'
rescue LoadError
require 'gem_name'
end
## app/controllers/users_controller.rb
class UsersController < ApplicationController::Base
def create
@user = User.new(params[:user])
if @user.save
SomeMailer.deliver_activation_email(@user, self) # pass in the controller object
redirect_to some_path
end
end
end
#!/usr/bin/ruby
class Foo
attr_accessor :name, :age
def initialize
@options = {:name => "Tim", :age => 28}
end
def work(h)
# Adds a convienence method to the Hash class
# Allows you to check if a hash has a valid key and value.
class Hash
def assert_valid_key?(key)
self.symbolize_keys.has_key?(key.to_sym) && !self[key].blank?
end
end
# Test it
class String
def to_hex
hex_chars = []
self.size.times do |n|
char = self.slice(n,n+1)
hex_chars.push(char.unpack('U')[0].to_s(16).upcase)
end
hex_chars.join(" ")
end
end
@timmatheson
timmatheson / heroku_logger.rb
Created September 3, 2010 23:32
HerokuLogger Class, allows you to tail the heroku logs through hackery.
class HerokuLogger
def initialize
@keep_processing = true
@log = nil
stop_tailing = Proc.new do
puts "---------------------- Terminating ------------------------"
@keep_processing = false
end
@timmatheson
timmatheson / moving_average.rb
Created October 16, 2010 04:03
Moving Averages for the Ruby Array Class
# Examples
# [1,1,2,1,1,2,2].moving_average(2) => [1, 1, 1, 2]
class Array
# Calculates the moving average
# given a Integer as a increment period
def moving_average(increment = 1)
return self.average if increment == 1
a = self.dup
result = []