Skip to content

Instantly share code, notes, and snippets.

# UPDATE: I benchmarked parsing JSON and it's way way faster. I'm going to transition to requesting
# JSON from flickr and store the cached data as JSON too.
#
# With Rreset (http://github.com/javan/rreset/tree/master), I am caching some of the XML
# returned from Flickr API calls. Then, on subsequent hits, the cached XML is loaded and parsed
# using Hash.from_xml which is an implementation of XmlSimple. I was curious if it would be
# faster to first convert the XML to Yaml and parse that on cached hits instead. Yaml was the
# obvious winner. There are probably way faster XML parsing libraries out there,
# but I wanted to stick with the Rails defaults.
#
sudo port install ruby
# Install rubygems 1.2.0 because the memcached gem requires it (macport installs 1.3.0)
curl -O http://files.rubyforge.mmmultiworks.com/rubygems/rubygems-1.2.0.tgz
tar xzvf rubygems-1.2.0.tgz
cd rubygems-1.2.0
sudo ruby setup.rb
#memcached
wget http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gz
<% if 1 == 1 # some comment %>
In ruby 1.8.6 you'll see me. In 1.8.7 you won't. It seems 1.8.7 doesn't like comments in erb tags like the one above.
<% end %>
class User < ActiveRecord::Base
# These are all boolean fields defined for this User model.
# As we add more email columns, and add them to this array,
# the named_scope below keeps up.
EMAIL_COLUMNS = [:email_new_market_alerts, :email_new_announcements, :email_stock_notices]
named_scope :unsubscribed, lambda { { :conditions => EMAIL_COLUMNS.inject({}) { |conditions, column| conditions[column] = false; conditions } } }
end
# Takes a collection of Stocks and moves the closed ones to the end
def closed_stocks_last(stocks)
open, closed = [], []
stocks.each do |stock|
if stock.closed?
closed << stock
else
open << stock
end
# Sorry for the confusing stock names. Their name does not indicate the order they should be in. "second" is the only closed stock.
# The original ordering
>> stocks.map(&:name)
=> ["second", "third", "first"]
# Ordering from #sort_by
>> stocks.sort_by { |s| s.closed? ? 1 : 0 }.map(&:name)
=> ["first", "third", "second"]
irb(main):007:0> 0 / 0
ZeroDivisionError: divided by 0
from (irb):7:in `/'
from (irb):7
from :0
irb(main):006:0> 0.0 / 0
=> NaN
irb(main):014:0> 0.0 == 0
<link href="jbar.css" media="screen" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="jquery.jbar.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(function(){
jQuery('.jbar').jbar();
});
</script>
<ul class="jbar">
# Tweet::OAuth::CONSUMER_KEY and Tweet::OAuth::CONSUMER_SECRET are strings I stored locally.
class OauthTwitterController < ApplicationController
# Will redirect to authorize with twitter
def show
oauth.set_callback_url(new_oauth_twitter_url)
session[:twitter_oauth_request_token] = oauth.request_token.token
session[:twitter_oauth_request_token_secret] = oauth.request_token.secret
@javan
javan / schedule.rb
Created December 19, 2010 22:57
Example schedule file for the Whenever gem.
every 1.day, :at => '2:30pm' do
runner 'Company.grow'
end
every 20.minutes do
command 'cd /home/fetch/fetches && wget http://example.com/latest_file.txt'
end
every :reboot do
rake 'memcached:start'