Skip to content

Instantly share code, notes, and snippets.

@scottlowe
scottlowe / memoize_alias.rb
Created August 6, 2011 22:41
Memoize with alias, redefine
def expensive_stats(id)
puts "Executing resource intensive method for ID: #{id}"
sleep 5
Time.now
end
alias :expensive_stats! :expensive_stats
def expensive_stats(id)
@expensive_stats ||= []
@scottlowe
scottlowe / memoize.rb
Created August 6, 2011 21:50
Memoize with a begin block
def expensive_stats(id)
@expensive_stats ||= []
@expensive_stats[id] ||= begin
puts "Executing resource intensive method for ID: #{id}"
sleep 5
Time.now
end
end
@scottlowe
scottlowe / fib.clj
Created July 16, 2011 23:13
Clojure function which returns the first X fibonacci numbers
(defn fib [max]
(loop [a 1, b 1, acc [1]]
(if (>= (count acc) max)
acc
(recur b (+ a b) (conj acc b)))))
@scottlowe
scottlowe / article.rb
Created April 27, 2011 21:00
Model code for Postgres full text search example
class Article < ActiveRecord::Base
validates :title, :body, :presence => true
# Note that ActiveRecord ARel from() doesn't appear to accommodate "?"
# param placeholder, hence the need for manual parameter sanitization
def self.tsearch_query(search_terms, limit = query_limit)
words = sanitize(search_terms.scan(/\w+/) * "|")
Article.from("articles, to_tsquery('pg_catalog.english', #{words}) as q").
where("tsv @@ q").order("ts_rank_cd(tsv, q) DESC").limit(limit)
@scottlowe
scottlowe / article_migration_snippet.rb
Created April 27, 2011 20:55
Migration to create Article model in text search example code
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.string :title
t.text :body
t.timestamps
end
end
@scottlowe
scottlowe / tsearch_migration.sql
Created April 27, 2011 20:51
SQL required to create a TSVECTOR column with associated triggers and indexes
-- Add the new tsvector column
ALTER TABLE articles ADD COLUMN tsv tsvector;
-- Create a function that will generate a tsvector from text data found in both the
-- title and body columns, but give a higher relevancy rating 'A' to the title data
CREATE FUNCTION articles_generate_tsvector() RETURNS trigger AS $$
begin
new.tsv :=
setweight(to_tsvector('pg_catalog.english', coalesce(new.title,'')), 'A') ||
setweight(to_tsvector('pg_catalog.english', coalesce(new.body,'')), 'B');
@scottlowe
scottlowe / tsearch_example.sql
Created April 27, 2011 20:30
SQL generated by ARel query in example app
SELECT title, body, id
FROM articles, to_tsquery('pg_catalog.english', 'hot|monkeys') as q
WHERE (tsv @@ q)
ORDER BY ts_rank_cd(tsv, q) DESC
LIMIT 25
@scottlowe
scottlowe / discover.rb
Created March 7, 2011 15:43
My copy of /autotest/discover.rb. Used to run Autotest with RSpec 2 and autotest-fsevent
Autotest.add_discovery { "rails" }
Autotest.add_discovery { "rspec2" }
# Save CPU cycles!
require 'autotest/fsevent' if RUBY_PLATFORM.include?('darwin')
@scottlowe
scottlowe / Another JRuby bactkrace option
Created February 13, 2011 19:17
Yet another JRuby backtrace alternative
*** Bad file descriptor - Bad file descriptor (Errno::EBADF)
--> `initialize' 944 org/jruby/RubyIO.java
`initialize' 424 org/jruby/RubyFile.java
`open' 1074 org/jruby/RubyIO.java
`__file__' 1 -e
`rbCatch' 1191 org/jruby/RubyKernel.java
`__file__' 1 -e
`times' 252 org/jruby/RubyFixnum.java
`(root)' 1 -e
@scottlowe
scottlowe / Alternative jruby backtrace.txt
Created February 13, 2011 19:02
An alternative JRuby backtrace version 4
*** Bad file descriptor - Bad file descriptor (Errno::EBADF)
==> `initialize' 944, org.jruby.RubyIO.java
`initialize' 424, org.jruby.RubyFile.java
`open' 1074, org.jruby.RubyIO.java
`__file__' 1, -e
`rbCatch' 1191, org.jruby.RubyKernel.java
`__file__' 1, -e
`times' 252, org.jruby.RubyFixnum.java
`(root)' 1, -e