Skip to content

Instantly share code, notes, and snippets.

def assert_equal(expected, actual)
if expected == actual
puts "Joy"
else
puts "Expected #{expected}, got #{actual}"
end
end
each_return = [1,2,3,4].each{|num| num * 5 }
assert_equal [1,2,3,4], each_return
desc "Create a bunch of seed data for artists and songs"
task :seed_artists_and_songs => [:environment, :clear_artists_and_songs] do
# Build Song Off Artist
# Given a Song called R.E.S.P.E.C.T
# build the Aretha Franklin Artist
s = Song.create(:name => "R.E.S.P.E.C.T")
s.build_artist(:name => "Aretha Franklin")
s.save
@aviflombaum
aviflombaum / date_formats.rake
Created March 28, 2012 20:49 — forked from lmarlow/date_formats.rake
Show available format strings for Date, DateTime, and Time objects in Rails
desc "Show the date/time format strings defined and example output"
task :date_formats => :environment do
now = Time.now
[:to_date, :to_datetime, :to_time].each do |conv_meth|
obj = now.send(conv_meth)
puts obj.class.name
puts "=" * obj.class.name.length
name_and_fmts = obj.class::DATE_FORMATS.map { |k, v| [k, %Q('#{String === v ? v : '&proc'}')] }
max_name_size = name_and_fmts.map { |k, _| k.to_s.length }.max + 2
max_fmt_size = name_and_fmts.map { |_, v| v.length }.max + 1
@aviflombaum
aviflombaum / gist:1867008
Created February 20, 2012 01:23
Observers and RSpec
require 'spec_helper'
describe AnswerObserver do
# let(:user) {stub_model(User)}
# let(:answer) {Factory(:answer, :user => user)}
describe '#after_create' do
it "calls update_answer_summary on the answer's user" do
# This tests the implied implentation that the observer's
# after_create will fire on a real answer
@aviflombaum
aviflombaum / gist:1865381
Last active September 30, 2015 21:28
Association Examples
# Assign the Artist to the Song
artist = Artist.create()
song = Song.create()
song.artist = artist
song.save
artist.reload
artist.songs
# Build the Song from the Artist
artist = Artist.create()
require 'spec_helper'
describe ProfilesController do
context "as a User" do
before(:each) do
@user = Factory(:user)
login_as(@user)
end
context "create a Profile" do
@aviflombaum
aviflombaum / config.ru
Created November 1, 2011 21:20
config.ru for static sites on heroku.
use Rack::Static, :urls => ["/stylesheets", "/images"], :root => "public"
run lambda { |env| [200, { 'Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=86400' }, File.open('public/index.html', File::RDONLY)] }
@aviflombaum
aviflombaum / gist:777013
Created January 12, 2011 22:14
igrigorik collection of things about ruby 1.9 that make him standup
#ruby19 added ObjectSpace.count_objects method which returns a hash of types+counts of objects in current process #standup
#ruby19 allows default parameters in procs & lambdas! ex: f = -> a,b=1,*c { p [a,b,c] }; f.call(1); f.(1,2); f[1,2,3] #standup
#ruby19 no longer supports .each on a string! instead, use: .chars, .bytes, .lines #standup
#ruby19 supports 3 ways to call a proc! ex: f =->n {[:hello, n]}; f[:ruby]; f.call(:ruby); f.(:ruby) #standup
#ruby19 hashes preserve the insertion order of elements! for the curious: http://bit.ly/e0oEun #standup
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
function superprompt {
local RED="\[\033[0;31m\]"
local LIGHT_RED="\[\033[1;31m\]"
export PS1="\[\e]2;\u@\h\a[\[\e[37;44;1m\]\t\[\e[0m\]]$RED\$(parse_git_branch) \[\e[32m\]\W\[\e[0m\] \$ "
PS2='> '
require 'rubygems'
require 'yaml'
require 'irb/completion'
alias q exit
class Object
def local_methods
(methods - Object.instance_methods).sort
end