Symbols vs Strings April 22nd, 2008

When to use Symbols and when to use Strings in Ruby is really a personal preference. Most of the time I program without taking the difference in performance in consideration.

I just use a few simple rules

in a hash it’s always :key => ‘value’

redirect_to :controller => 'pages', :action => 'index'

special keywords are symbols

Page.find(:all)
Page.find(:first)
redirect_to(:back)

attributes and status’es are symbols

update_attribute(:name, 'Jan')
comment.state = :approved

Except for those rules it’s really what feels right while coding it.

tags: l 1 comments »