This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ||= [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def expensive_stats(id) | |
@expensive_stats ||= [] | |
@expensive_stats[id] ||= begin | |
puts "Executing resource intensive method for ID: #{id}" | |
sleep 5 | |
Time.now | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn fib [max] | |
(loop [a 1, b 1, acc [1]] | |
(if (>= (count acc) max) | |
acc | |
(recur b (+ a b) (conj acc b))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateArticles < ActiveRecord::Migration | |
def self.up | |
create_table :articles do |t| | |
t.string :title | |
t.text :body | |
t.timestamps | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Autotest.add_discovery { "rails" } | |
Autotest.add_discovery { "rspec2" } | |
# Save CPU cycles! | |
require 'autotest/fsevent' if RUBY_PLATFORM.include?('darwin') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*** 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*** 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 |