Introduction
Sinatra is an open-source web application framework of Ruby. It is an alternative to Ruby on Rails among the other web application frameworks based on Ruby. It is more robust and hence requires more work and time for development.
This article will discuss scopes and binding in Sinatra. We will illustrate the concept of scopes and bindings with examples.

Scope in Sinatra
The scope is a crucial concept in Sinatra. It can help developers understand errors and eliminate confusion. Scope defines an area where certain variables can be accessed at any time in a program.
Scope in Sinatra is static and called lexical scope. It depends upon the structure of the program. It changes with blocks, classes, and methods. There are generally three ways you can categorize scopes in Sinatra.

They are:
- Local Variable Scope
- Instance Variable Scope
- Scopes in Blocks
We will talk about each scope type in detail now.
Local Variable Scope
Methods have local variables created when the method is invoked and deleted once it returns. It is the narrowest scope.
Example 1
var = 25
def demo_method
var = 100
puts var
end
demo_method
puts varOutput

Explanation
Here the variable var is defined outside the class as 25, and when we enter the method demo_method, a new variable var is created with the value 100. So the var variable inside the method differs from the var variable outside the class. So when it is printed from inside the class, the value 100 is printed, and when we print it from outside the class, the value 25 is printed.
If we haven’t defined another var variable inside the class, then NameError will be raised as no var variable will be found inside the method's scope.
Example 2
var = 25
def demo_method
puts var
end
demo_methodOutput

Instance Variable Scope
It has a broader scope. It is used to share information inside a Sinatra object. Instance variables are associated with instances(objects) of a class and are shared among all the methods invoked by that object. However, different objects have different instance variables.
Example 3
class Nationality
def Indian
@var = "indian"
puts @var
end
def American
@var = "american"
puts @var
end
end
Syna = Nationality.new
Johnson = Nationality.new
Syna.Indian
Johnson.AmericanOutput

Explanation
Here, the @var variable in the Indian and American Methods is the same. But different objects (Syna and Johnson) have different variables as we have used different methods with both objects.
Example 4
class Employee
def base_salary
@salary = 15000
puts @salary
end
def salary_with_bonus
@salary = @salary + 2000
puts @salary
end
end
Syna = Employee.new
Syna.base_salary
Syna.salary_with_bonusOutput

Explanation
The @salary variable is shared among the base_salary and salary_with_bonus methods as it’s an instance variable.
Scopes in Blocks
Blocks make things interesting when it comes to scope. Local variables can be carried inside a block, and we can also change its values. But the local variables created inside a block are not carried outside and get deleted when we come out of the block.
Example 5
list = []
5.times { list << "Hurray" }
p listOutput

Explanation
Here the local variable list is carried inside the block times, and “Hurray” is inserted 5 times. The variable list can be accessed from outside the block and still print 5 “Hurray” messages.
Example 6
5.times { list = []
list << "Hurray" }
p listOutput

Explanation
We have not defined the list variable outside the block, so a new list variable is created inside the block. When we try to access the list variable from outside the block, NameError is raised.








