Skip to content

Instantly share code, notes, and snippets.

require 'rubygems'
require 'stemmer'
require 'classifier'
class LedeClassifier
attr :classifier
def initialize(sections, n)
@classifier = Classifier::Bayes.new(*sections)
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
(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))]))
@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:
#
# Any Model
def to_s
name
end
def to_param
"#{id}-#{to_s.parameterize}"
end
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
module FlashHelper
def flashes
{:partial => '/forms/flashes', :object => flash}
end
def flash_names
[:notice, :error, :message, :success]
end
@mischa
mischa / railroad.rb
Created December 16, 2008 02:52
Run Railroad as a rake task
#(Thanks to Thomas Ritz, http://www.galaxy-ritz.de ,for the code.)
namespace :doc do
namespace :diagram do
task :models do
sh "railroad -i -l -a -m -M | dot -Tsvg | sed 's/font-size:14.00/font-size:11.00/g' > doc/models.svg"
end
task :controllers do
sh "railroad -i -l -C | neato -Tsvg | sed 's/font-size:14.00/font-size:11.00/g' > doc/controllers.svg"
if [child, parent, grandpa, president, food, ink].all{|e| e.valid?}
... #do stuff
end
>> %w{foo, bar, baz}.all?{|e| e.is_a?(String)}
=> true
>> ["foo", "bar", 1].all?{|e| e.is_a?(String)}
=> false