Table of contents
1.
Introduction
2.
Threads for Concurrency
3.
Fibres for Coroutines
4.
Continuations
4.1.
Execution of Block
4.2.
Demonstration of Continuations
5.
Frequently Asked Questions
5.1.
Name the different types of Ruby’s control structure.
5.2.
Define the term thread.
5.3.
Explain the use of coroutines.
5.4.
How does the execution of the block take place?
5.5.
Name the class which automatically converts internal iterators.
6.
Conclusion
Last Updated: Mar 27, 2024

Continuations In Ruby

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

Introduction

This blog introduces threads, which are Ruby’s control structure for concurrent execution, and two more esoteric control structures called fibres and continuations.

Threads for Concurrency

A thread of execution is a sequence of Ruby statements that run (or appear to run) in parallel with the primary series of reports the interpreter runs. Thread objects represent threads, but they can also be thought of as control structures for concurrency.

Fibres for Coroutines

The name “fibre” has been used elsewhere for a kind of lightweight thread, but Ruby’s fibres are better described as coroutines or, more accurately, semicoroutines. 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 next result. In Ruby, the Fiber class automatically converts internal iterators, such as each method, into enumerators or external iterators.

Note that fibres are an advanced and relatively obscure control structure; most Ruby programmers will never need to use the Fiber class directly.

Continuations

A continuation is another complex and obscure control structure that most programmers will never need to use. A continuation takes the form of the Kernel method callcc and the Continuation object. Continuations are part of the core platform in Ruby 1.8, but they have been replaced by fibres and moved to the standard library in Ruby 1.9.

To use them in Ruby 1.9, you must explicitly require them with:

require ‘continuation’

Execution of Block

The Kernel method callcc executes its block, passing a newly created Continuation object as the only argument. The Continuation object has a call method, which returns the callcc invocation to its caller. The value passed to call becomes the return value of the callcc devotion. In this sense, callcc is like catch, and the call method of the Continuation object is like thra ow. Continuations are different because the Continuation object can be saved into a variable outside of the callcc block. The call method of this object may be called repeatedly and causes control to jump to the first statement following the callcc invocation.

Demonstration of Continuations

The following code demonstrates how continuations can be used to define a method that works like the goto statement in the BASIC programming language:

# Global hash for mapping line numbers (or symbols) to continuations
 $lines = {} 
# Create a continuation and map it to the specified line number 
def line(symbol)
 callcc {|c| $lines[symbol] = c } 
end 
# Look up the continuation associated with the number, and jump there
def goto(symbol) 
 $lines[symbol].call 
end 
# Now we can pretend we're programming in BASIC
i = 0 
line 20                      # Declare this spot to be line 20 
puts i += 1
goto 20 if i < 5         # Jump back to line 20 if the condition is met
 line 30                    # Declare this spot to be line 30 
puts i -= 1 
goto 30 if i >0 
You can also try this code with Online Ruby Compiler
Run Code

Frequently Asked Questions

Name the different types of Ruby’s control structure.

The three types of Ruby’s control structures are threads, fibres, and continuations.

Define the term thread.

A thread of execution is a sequence of Ruby statements that run (or appear to run) in parallel with the primary series of reports the interpreter runs.

Explain the use of 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 next result. 

How does the execution of the block take place?

The Kernel method callcc executes its block, passing a newly created Continuation object as the only argument. The Continuation object has a call method, which returns the call invocation to its caller. 

Name the class which automatically converts internal iterators.

Fibre Class automatically converts internal iterators.

Conclusion

After reading about Continuations in Ruby, are you not feeling excited to read/explore more articles on continuations? 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