I was intrigued by the simplicity and elegance of Sinatra as presented by Bence Golda at the last budapest.rb meetup., so I decided to build a simple application with it (I am planning to talk just a little bit more about that in a later post).
One quick thing I learned the hard way is when generating haml views in Sinatra, you have to use symbols as the name of the view, strings will just be rendered as strings. That was a tough lesson to learn coming from Rails. So while in Rails you can write:
def index render :action => :index end
def index render :action => "index" end
In Sinatra, you can only use a symbol to designate the view to be rendered:
get '/' do haml :index end
Which makes sense, I think, since symbols (unlike strings, see below) with the same name refer to the same entity which is a perfect fit for naming views, now ain’t they? Plus, I’ll admit I am in love with them symbols, with their colon in the front and all. And when they are syntax highlighted, I melt.
irb(main):002:0> sym1 = :index; sym2 = :index; sym1.object_id == sym2.object_id => true irb(main):003:0> s1 = "index"; s2 = "index"; s1.object_id == s2.object_id => false