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!