Table of contents
1.
Introduction 
2.
Scope in Sinatra
2.1.
Local Variable Scope
2.1.1.
Example 1
2.1.2.
Output
2.1.3.
Explanation
2.1.4.
Example 2
2.1.5.
Output
2.2.
Instance Variable Scope 
2.2.1.
Example 3
2.2.2.
Output
2.2.3.
Explanation
2.2.4.
Example 4
2.2.5.
Output
2.2.6.
Explanation
2.3.
Scopes in Blocks
2.3.1.
Example 5
2.3.2.
Output
2.3.3.
Explanation
2.3.4.
Example 6
2.3.5.
Output
2.3.6.
Explanation
3.
Bindings in Sinatra
3.1.
Example 7
3.2.
Output
3.3.
Explanation
3.4.
Example 8
3.5.
Output
4.
Frequently Asked Questions
4.1.
What is Sinatra?
4.2.
How is Sinatra different from Ruby on Rails?
4.3.
What is a scope in Sinatra?
4.4.
What is RubyGems in Ruby?
4.5.
What are bindings in Sinatra?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Scopes and Binding in Sinatra

Author Teesha Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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 and bindings in Sinatra

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. 

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 var
You can also try this code with Online Ruby Compiler
Run Code

Output

Output1

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_method
You can also try this code with Online Ruby Compiler
Run Code

Output

Output2

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.American
You can also try this code with Online Ruby Compiler
Run Code

Output

Output3

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_bonus
You can also try this code with Online Ruby Compiler
Run Code

Output

Output4

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 list
You can also try this code with Online Ruby Compiler
Run Code

Output

Output5

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 list
You can also try this code with Online Ruby Compiler
Run Code

Output

Output6

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. 

Bindings in Sinatra

Bindings in Sinatra are used to store the information about the scope at any point of time in the program. The binding method in Sinatra returns a Binding object that contains all the variables available at the current scope. 

Bindings

To evaluate a method or variable, you can use the eval method. 

Example 7

var = 5
def demo_method
    var2 = 10
    var3 = 15
end
var4 = 25

puts binding.local_variables 
You can also try this code with Online Ruby Compiler
Run Code

Output

Output7

Explanation

The Binding object is outside the scope of the method demo_method, so the variables defined inside the demo_method are not included in the binding object.

Example 8

var = 5
def demo_method
    var2 = 10
    var3 = 15
end

var4 = 25
puts binding.eval("var") 
puts binding.eval("var2")
You can also try this code with Online Ruby Compiler
Run Code

Output

Output8

Frequently Asked Questions

What is Sinatra?

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.

How is Sinatra different from Ruby on Rails?

Sinatra is more robust; you can build more customized web applications with Sinatra compared to Ruby on Rails. Hence with Sinatra, you have to work more, requiring more development time. 

What is a scope in Sinatra?

Scope defines an area where certain variables can be accessed at any point of 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.

What is RubyGems in Ruby?

RubyGems is a standard distribution mechanism for Ruby applications and libraries. It's a Ruby programming language package manager. Since Ruby version 1.9.c, RubyGems has been included in the standard library.

What are bindings in Sinatra?

Bindings in Sinatra are used to store the information about the scope at any point of time in the program. The binding method in Sinatra returns a Binding object that contains all the variables available at the current scope. 

Conclusion

This article has discussed scopes and bindings in Sinatra. We have illustrated the concept of scopes and bindings by giving several examples.

I hope you would have gained a better understanding of these topics now!

Are you planning to ace the interviews of reputed product-based companies like AmazonGoogleMicrosoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Thank You

Live masterclass