Table of contents
1.
Introduction
2.
Applications of Ruby
3.
What is Variable Scope?
4.
Local Variable Scope
5.
Instance Variable Scope
6.
What is a Block Scope in Ruby?
7.
Frequently Asked Questions
7.1.
Why do we use blocks in Ruby?
7.2.
What are Ruby lambdas?
7.3.
What is a singleton class in Ruby?
7.4.
Define yield in Ruby?
7.5.
What is meta class in Ruby?
8.
Conclusion
Last Updated: Mar 27, 2024

Block and Variable Scope in Ruby

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

Introduction

Ruby is unique and one of the most popular programming languages because of the capabilities it offers. Yukihiro Matsumoto created Ruby in 1995. It is an interpreted, dynamic, and entirely object-oriented language. The primary motivation for developing Ruby was to have a language that could handle data with simple syntax and issues with logic.

Applications of Ruby

  • Desktop applications
  • Static websites
  • Data processing tools
  • Automation tools

Let's learn Block and Variable Scopes in Ruby now that you've had a brief introduction to the language.

What is Variable Scope?

  • The term "scope" describes the available variables at any particular time.
  • The scopes of various types of variables vary differently.
  • A scope might be extremely limited (local variables) or quite expansive (global variables).
  • To prevent issues with state mutation and name collision, you should utilize the narrowest scope possible.
  • All Ruby developers should be aware of the idea and importance of the scope.

Local Variable Scope

The scope of a local variable is the smallest among all. Particularly, local variables set up within methods are destroyed after the method completes.

val = 50
def myFunc
 val = 100
 puts val
end


What do you think myFunc will print?

myFunc will always print 100 when you call it. The val variable inside the method is unaffected by the fact that an is specified externally as 50.

They are different variables with the same name and different scopes.

Intuition:

The scope can be compared to a bubble of soap.

When you call a method, you enter a new EMPTY bubble after the initial a = 50 has been entered.

There are no local variables that you introduce into the new bubble.

And when the process is completed...

The bubble bursts.

You can no longer access the variables inside the bubble because they vanish.

Local variables operate in this manner.

Instance Variable Scope

Instance variables have a wider scope as compared to other variables. Specifically, they are primarily used for sharing data inside a Ruby object.

class Cars
 Def sedan
   @quantity = 100
 end
 def suv
   @quantity = 60
 end
end


In the above example, @quantity is the same variable for both the sedan & suv methods.

It's a shared value between the methods. But outside the same class, and even on different Car objects, @quantity is going to be different. For Example

bmw = Car.new
mahindra  = Car.new
bmw.sedan
mahindra.suv


As we know, every object has its own set of instance variables.

So in the above example, bmw is going to have a @quantity of 100, and mahindra is going to have a @quantity of 60.

Just like different persons have different ages, names, DNA, country, etc.

What is a Block Scope in Ruby?

Blocks are very interesting and useful when it comes to the scope. They extend the concept of scope as compared to variables.

If we follow our bubble analogy from variable scope again, what a block does is bring over local variables from the current bubble and let you access & change them.

One thing to notice is that the bubble still pops, removing any NEW local variables that were created inside the block.

arr = []
4.times { arr << 2 }
p arr

Output:

[2,2,2,2]

I hope now you have got a fair idea of how Scopes vary in Ruby. Let's move to the FAQs.

Frequently Asked Questions

Why do we use blocks in Ruby?

Blocks, which resemble closures, have been a feature of the Ruby language since its beginning. They can minimize repetition and even make coding less prone to errors when used correctly.

What are Ruby lambdas?

A lambda is an object in Ruby that is comparable to a proc. A lambda returns to its calling procedure rather than exiting immediately, and unlike a proc, it needs a particular amount of arguments supplied to it.

What is a singleton class in Ruby?

A creational design pattern called singleton makes sure that there is only one object of its sort and gives all other code a single point of access to it.

Define yield in Ruby?

The Ruby keyword yield enables developers to provide arguments to blocks from the yield; there are no restrictions on the number of arguments that can be passed to a block.

What is meta class in Ruby?

Ruby automatically constructs a class to house just that function when you declare a singleton method on an object.

Conclusion

In this article, we have extensively discussed the Block Syntax in Ruby and various other ways to implement the Block in Ruby.

To learn more about  Ruby and its applications, see Ruby DocumentationRuby By Coding NinjasRuby Documentation-BlockRuby FAQ, and Ruby Archives.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass