Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Building Web Apps with Sinatra ERB Templates Embedding Page Data with ERB

Why use @title and @content? Why not just title and content?

The block isn't a class so ... why put the @ in front?

2 Answers

Hi Min Jun Kim ,

I haven't worked with Sinatra before but it looks like there are 2 styles of writing the application, classic and modular. The approach taken in the video is the classic style.

Here's a blog post showing both styles: http://blog.carbonfive.com/2013/06/24/sinatra-best-practices-part-one/

Looking at the modular style, you can see that you create a class that inherits from the sinatra base class and your routes go inside there. Writing it this way, the class is explicit and it's more obvious why you would be able to use instance variables in your routes because it's all inside a class.

With the classic style, you're still working with a class but it's implicit. It's something that's taken care of behind the scenes. The class methods are exported to the top level.

There's some parts of the documentation and code that explain a little bit about how it works.

The "Writing Extensions" part of the documentation has some information about how sinatra works internally. http://www.sinatrarb.com/extensions.html

From the sinatra base source code (around lines 1912 to 1918) https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb

# Execution context for classic style (top-level) applications. All
# DSL methods executed on main are delegated to this class.
#
# The Application class should not be subclassed, unless you want to
# inherit all settings, routes, handlers, and error pages from the
# top-level. Subclassing Sinatra::Base is highly recommended for
# modular applications.

Those are the comments for the Application class which is what you're using behind the scenes when you use the classic style.

Thanks so much for the thorough response!

This is an excellent answer. I was wondering the exact same thing. Thank you Jason Anello !