The status_update Plugin

Posted by Chris Blackburn Wed, 25 Jun 2008 17:41:00 GMT

I’ll be publishing the status_update plugin for Rails in the coming weeks. The plugin is an interface to Ping.fm, the cool new service that allows you to update all of your social network statuses with a single API.

Coming Soon…

Posted in  | Tags , , , , ,  | no comments | no trackbacks

State Selector From Collection or State Model

Posted by Chris Blackburn Thu, 19 Jun 2008 01:01:00 GMT

schema.rb for states table
create_table "states", :force => true do |t|
  t.string   "name"
  t.string   "abbreviation", :limit => 2
  t.datetime "created_at"
  t.datetime "updated_at"
end
cities_controller.rb
# GET /cities/new
# GET /cities/new.xml
def new
  @city = City.new
  @states = State.find(:all, :order => :abbreviation)

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @city }
  end
end
new.html.erb view for cities_controller.rb
<% form_for(@city) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :state_id %><br />
    <%= f.collection_select(:state_id, @states, "id", "abbreviation") %>
  </p>
  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

Posted in  | Tags , , , , , ,  | no comments | no trackbacks

Esoteric Subversion Commands

Posted by Chris Blackburn Sun, 15 Jun 2008 21:42:00 GMT

Make a file executable, aka. set the execute/executable bit on a file using subversion:

svn ps svn:executable true path/to/filename

Ignore a file or file glob:

svn ps svn:ignore "*.pattern" path/to/directory

Merge two repositories:

svn merge --ignore-ancestry "$prod@HEAD" "$branch@HEAD" "$path"

example:

@svn merge svn://svn.cbciweb.com/svn/fakepath/trunk svn://svn.cbciweb.com/svn/fakepath/branches/stage /home/cblackburn/src/ruby/fake_project@

... after the merge don’t forget to commit.

Posted in  | Tags , , , , , ,  | no comments | no trackbacks

Ruby Performance :: Use Double Quotes vs. Single Quotes

Posted by Chris Blackburn Tue, 10 Jun 2008 17:31:00 GMT

Regarding Ruby strings, surprisingly, embedding substitutions in double quoted strings perform better than using the single-quoted strings and the array append operator

require 'profilings'
include PeepcodeProfiler

MAX = 900000

###
# Quoted Strings
###

time_this("Single Quotes (append):") {
  x = ''
  (0..MAX).each do |i|
    x = 'This is a test ' << i.to_s
  end
}
Timings for Single Quotes (append):
Thread ID: 218880
Total: 11.150243

 %self     total     self     wait    child    calls  name
 58.47     11.15     6.52     0.00     4.63        1  Range#each (ruby_runtime:0}
 20.99      2.34     2.34     0.00     0.00   900001  String#<< (ruby_runtime:0}
 20.55      2.29     2.29     0.00     0.00   900001  Fixnum#to_s (ruby_runtime:0}
  0.00      0.00     0.00     0.00     0.00        1  <Class::Object>#allocate (ruby_runtime:0}
  0.00     11.15     0.00     0.00    11.15        0  PeepcodeProfiler#time_this (./profilings.rb:8}

... and the faster double-quotes append:

time_this("Double Quotes (append):") {
  x = ""
  (0..MAX).each do |i|
    x = "This is a test #{i}"
  end
}
Timings for Double Quotes (append):
Thread ID: 218880
Total: 7.385500

 %self     total     self     wait    child    calls  name
 66.64      7.39     4.92     0.00     2.46        1  Range#each (ruby_runtime:0}
 33.36      2.46     2.46     0.00     0.00   900001  Fixnum#to_s (ruby_runtime:0}
  0.00      0.00     0.00     0.00     0.00        1  <Class::Object>#allocate (ruby_runtime:0}
  0.00      7.39     0.00     0.00     7.39        0  PeepcodeProfiler#time_this (./profilings.rb:8}

Even more interesting, is how the string substitution barely blinks when it is in the middle of the string vs. at the end. To get the same effect using 2 appends with single-quoted strings, takes twice as long as double-quoted strings with substitution in the middle:

# Very slow
# BEGIN single_quotes_middle
time_this("Single Quotes: (in middle)") {
  x = ''
  (0..MAX).each do |i|
    x = 'This is a test ' << i.to_s << 'x'
  end
}
# END single_quotes_middle
Timings for Single Quotes: (in middle)
Thread ID: 218880
Total: 15.146328

 %self     total     self     wait    child    calls  name
 56.51     15.15     8.56     0.00     6.59        1  Range#each (ruby_runtime:0}
 28.00      4.24     4.24     0.00     0.00  1800002  String#<< (ruby_runtime:0}
 15.49      2.35     2.35     0.00     0.00   900001  Fixnum#to_s (ruby_runtime:0}
  0.00      0.00     0.00     0.00     0.00        1  <Class::Object>#allocate (ruby_runtime:0}
  0.00     15.15     0.00     0.00    15.15        0  PeepcodeProfiler#time_this (./profilings.rb:8}
# BEGIN double_quotes_middle
time_this("Double Quotes (in middle):") {
  x = ""
  (0..MAX).each do |i|
    x = "This is a test #{i}x"
  end
}
# END double_quotes_middle
Timings for Double Quotes (in middle):
Thread ID: 218880
Total: 7.410465

 %self     total     self     wait    child    calls  name
 65.51      7.41     4.85     0.00     2.56        1  Range#each (ruby_runtime:0}
 34.49      2.56     2.56     0.00     0.00   900001  Fixnum#to_s (ruby_runtime:0}
  0.00      0.00     0.00     0.00     0.00        1  <Class::Object>#allocate (ruby_runtime:0}
  0.00      7.41     0.00     0.00     7.41        0  PeepcodeProfiler#time_this (./profilings.rb:8}

Look for my, soon-to-be-published, Peepcode book for more tips and recipes on how to scale Ruby on Rails.

Posted in  | Tags , , , ,  | 10 comments | no trackbacks