Skip to content

Instantly share code, notes, and snippets.

def link_to(*args, &block)
if block_given?
options = args.first || {}
html_options = args.second
concat(link_to(capture(&block), options, html_options))
else
name = args.first
options = args.second || {}
html_options = args.third
# Any Model
def to_s
name
end
def to_param
"#{id}-#{to_s.parameterize}"
end
@mischa
mischa / gist:38571
Created December 21, 2008 04:57
closures in ruby
# CLOSURES IN RUBY Paul Cantrell http://innig.net
# Email: username "cantrell", domain name "pobox.com"
# I recommend executing this file, then reading it alongside its output.
#
# Alteratively, you can give yourself a sort of Ruby test by deleting all the comments,
# then trying to guess the output of the code!
# A closure is a block of code which meets three criteria:
#
(define-struct time (hours minutes seconds))
(define (time-point hours minutes seconds)
(make-time hours minutes seconds))
(define (time-diff t1 t2)
(cond
[(> (seconds t1) (seconds t2)) ((- one-day (seconds t1)) + (seconds t2))]
[else (- (seconds t2) (seconds t1))]))
class MischaTime < Struct.new(:hours, :minutes, :seconds); end
def time_diff(t1, t2)
if seconds(t1) > seconds(t2)
(one_day - t1.seconds) + seconds(t2)
else
seconds(t2) - seconds(t1)
end
end
require 'rubygems'
require 'stemmer'
require 'classifier'
class LedeClassifier
attr :classifier
def initialize(sections, n)
@classifier = Classifier::Bayes.new(*sections)
#
# Why setting the default value of a Hash to be a Hash is wrong
#
# When I initialize a hash, setting the default value to be another hash,
# I am not able to set the value of the hash within the hash
#
# h = Hash.new({})
#
# h['bar']['baz] = 'foo' # Try to set the value of baz
#
# Inject
%w{earl-grey peach white}.inject({}) do |hash, string|
hash[string] = "tea"
hash
end
=> {"white"=>"tea", "earl-grey"=>"tea", "peach"=>"tea"}
# Each with object
>> %w{earl-grey peach white}.each_with_object({}){|string, hash| hash[string] = "tea"}
#features/steps/users_steps.rb:
Given "I'm a logged in member" do
@me = create_adult
logged_in_as @me
end
#features/support/env.rb:
class Cucumber::Rails::World