Skip to content

Instantly share code, notes, and snippets.

@jferris
jferris / surprise.rb
Created August 23, 2012 21:16
Principle of enjoyable surprises
test = test
# => nil
@test = @test
# => nil
$test = $test
# => nil
@@test = @@test
# NameError: uninitialized class variable @@test in Object
@jferris
jferris / _todo.html.erb
Created August 23, 2012 14:14
Testing views without helpers or decorators
<li class='<%= 'complete' if todo.complete? %>' id='<%= dom_id todo %>'>
<%= todo.description %>
<% if todo.completed_at? -%>
<%= link_to 'Incomplete', todo_completion_path(todo), method: :delete %>
<% else -%>
<%= link_to 'Complete', todo_completion_path(todo), method: :post %>
<% end -%>
</li>
@jferris
jferris / class_with_comment.rb
Created August 20, 2012 19:05
Example Class Comment
# This class does something simple enough that I can describe in one sentence.
#
# Sometimes you may want to elaborate a little bit in one or two more sentences.
#
# Example:
# BroomStick.new.quiddich_and_golden_snitches
class BroomStick
end
it 'finds posts containing the given word in the title' do
# The titles are in a different file than the test, so you need to flip back and forth to see
# what's actually going on here. When somebody adds another example post to the fixtures, this
# test will start failing, which is a distraction and a waste of time. We were also never able to
# come up with fixtures that weren't either hopelessly entangled or completely bloated from adding
# a specific fixture for almost every test.
Post.search('example').map(&:title).should =~ ['example one', 'example two']
end
it 'finds posts containing the given word in the title' do
# create_table :posts do |table|
# table.string :title, :null => false, :default => ''
# table.string :body, :null => false, :default => ''
# table.string :published, :default => false
# end
class Post < ActiveRecord::Base
attr_accessible :title, :body
validates_presence_of :title
validates_presence_of :body, :if => :published?
@jferris
jferris / benchmark.rb
Created December 5, 2011 15:20 — forked from sikachu/benchmark.rb
Compare between not define a method in subclass and define a method in subclass and call #super
require 'benchmark'
class Base
def foo
true
end
end
class Implicit < Base
end
@jferris
jferris / dependency_injection.rb
Created December 2, 2011 14:45 — forked from croaky/dependency_injection.rb
Dependency Injection in Ruby
module TaxCodeStrategies
class UnitedStates
def call(id)
"US-#{id}"
end
end
class Brazil
def call(id)
"#{id}-BR"
@jferris
jferris / time_formats.rb
Created November 16, 2011 17:05
Figure out what strtime characters are supported by your Ruby
#!/usr/bin/env ruby
require 'rubygems'
require 'active_support/core_ext'
chars = (('a'..'z').to_a + ('A'..'Z').to_a)
time = Time.now.beginning_of_month
twelve_hours_from_now = 12.hours.from_now
end_of_next_month = 1.month.from_now.end_of_month
@jferris
jferris / Gemfile
Created September 13, 2011 19:02
Guard setup for spork, passenger, and bundler
group :development, :test do
gem "spork", "~> 0.9.0.rc"
gem "guard"
gem "guard-spork"
gem "guard-bundler"
gem "guard-passenger"
gem "rb-fsevent", :require => false
gem "growl", :require => false
Factory.define :gallery do |f|
f.name "Gallery Name"
end
Factory.define :region do |f|
f.name "Telugu"
end