Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Fiber
3.
Fiber Arguments and Return Values
4.
Advanced Fiber Features
5.
Frequently Asked Questions
5.1.
What is the purpose of using fiber class?
5.2.
Define the role of callers.
5.3.
Explain the idea behind fiber. yield method.
5.4.
What are advanced fiber features?
5.5.
How to run a fiber?
6.
Conclusion
Last Updated: Mar 27, 2024

Fibers In Coroutines In Ruby

Author Prachi Singh
0 upvote

Introduction

Ruby 1.9 introduces a control structure known as Fiber and represented by an object of class Fiber. The name “fiber” has been used elsewhere for a kind of lightweight thread, but Ruby’s fibers are better described as coroutines or, more accurately, semi-coroutines. The most common use for coroutines is to implement generators: objects that can compute a partial result, yield the result back to the caller, and save the computation state so that the caller can resume that computation to obtain the following result. In Ruby, the Fiber class automatically converts internal iterators, such as each method, into enumerators or external iterators.

Fiber

Fiber has a body of code like a thread does. Create a fiber with Fiber. New, associate a block with it to specify the code the Fiber will run. Unlike a thread, the fiber body does not start executing immediately. To run a fiber, call the resume method of the Fiber object that represents it. The first time resume is called on a fiber; control is transferred to the beginning of the fiber body. That Fiber then runs until it reaches the end of the body or until it executes the class method Fiber. Yield. The Fiber .yield method transfers control back to the caller and make the call to resume return. It also saves the state of the Fiber so that the next call to continue makes the Fiber pick up where it left off.

Fiber Arguments and Return Values

Fibers and callers can exchange data through the arguments and return values of resume and yield. The views to the first call to resume are passed to the block associated with the fiber: they become the values of the block parameters. On subsequent calls, the arguments to resume become the return value of Fiber. yield. Conversely, any arguments to Fiber. yield becomes the return value of the resume. And when the block exits, the value of the last expression evaluated also becomes the return value of the resume.

In the caller’s code, the messages are always arguments to resume, and the responses are always the method's return value. In the fiber body, all letters but the first are received as the return value of Fiber. Yield and all responses but the last are passed as arguments to Fiber. yield.

Advanced Fiber Features

The fiber module in the standard library enables additional, more powerful features of the fibers. To use these features, you must: 

require 'fiber' 

However, you should avoid using these additional features wherever possible because: • All implementations do not support them. JRuby, for example, cannot support them on current Java VMs. • They are so powerful that misusing them can crash the Ruby VM. The core features of the Fiber class implement semi-coroutines. These are not true coroutines because there is a fundamental asymmetry between the caller and the fiber: the caller uses resume, and the fiber uses yield. However, if you require the fiber library, the Fiber class gets a transfer method that allows any fiber to transfer control to any other fiber. 

Frequently Asked Questions

What is the purpose of using fiber class?

 In Ruby, the Fiber class automatically converts internal iterators, such as each method, into enumerators or external iterators.

Define the role of callers.

Fibers and callers can exchange data through the arguments and return values of resume and yield.

Explain the idea behind fiber. yield method.

The Fiber .yield method transfers control back to the caller and make the call to resume return. It also saves the state of the Fiber so that the next call to continue makes the Fiber pick up where it left off.

What are advanced fiber features?

The fiber module in the standard library enables additional, more powerful features of the fibers. To use these features, you must: 

require 'fiber' 

How to run a fiber?

To run a fiber, call the resume method of the Fiber object that represents it. The first time resume is called on a fiber; control is transferred to the beginning of the fiber body. 

Conclusion

After reading about Fibers in Coroutines, are you not feeling excited to read/explore more articles on Fibers in Ruby? Don't worry; Coding Ninjas has you covered.

However, you may want to pursue our premium courses to give your job an advantage over the competition!

With our Coding Ninjas Studio Guided Path, you may learn about Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and more! Check out the mock test series and participate in the contests on Coding Ninjas Studio if you want to put your coding talents to the test! As part of your placement preparations, you must evaluate the obstaclesinterview experiences, and interview package in this case. Please vote for our blogs if you find them valuable and exciting.

Happy studying!!

Live masterclass