Table of contents
1.
Introduction 
2.
Tracing in Ruby 
2.1.
Keywords used for Tracing in Ruby
2.2.
Functions used for Tracing in Ruby
3.
Frequently Asked Questions 
3.1.
Why do programmers perform tracing?
3.2.
What is manual tracing in programming?
4.
Conclusion
Last Updated: Mar 27, 2024

Tracing in Rub

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

Introduction 

Code tracing is one of the most significant skills a programmer should master when learning to program. Code tracing is a way to trace the execution of a program or code segment in order to track the variable values as they change during execution and to determine the output of the code.  

Tracing in Ruby

So, in this article, we will discuss Tracing in Ruby

Tracing in Ruby 

Ruby defines a number of features for tracing the execution of a program. These are mainly useful for debugging code and printing informative error messages. They include keywords and methods.

Keywords used for Tracing in Ruby

For tracing, Ruby has two simplest keywords, namely - __LINE__ and __FILE__.  

__LINE__ evaluates the line number within the file where the error message appears, and __FILE__ specifies the name of the file in which the error message occurs.  

This way, these features enable an error message to specify the exact location at which it gets generated. It is generated as follows:

STDERR.puts "#{__FILE__}:#{__LINE__): invalid data" 
You can also try this code with Online Ruby Compiler
Run Code

Functions used for Tracing in Ruby

There are mainly three(3) functions generally used while Tracing in Ruby. They are - Object.instance_evalKernel.eval, and Module.class_eval. They take two arguments. These methods accept a line number and filename (basically, some string) as their arguments. To evaluate the code extracted of any sort from a file, extracted from the file of some sort, these arguments can be used to specify the values of __LINE__ and __FILE__ for the evaluation of these methods.

Whenever an exception is raised but not handled, the error message printed to the console contains the line number and filename information. This information is based on __LINE__ and __FILE__. Each Exception object has a backtrace related to it that tells where exactly it was raised,  where that method was invoked, where the method that raised the exception was invoked, and so on. 

The method Exception.backtrace returns an array of strings storing this information. The first element of this array is the location at which the exception occurred, and every subsequent element is one stack frame higher.  

An exception is not required to be created to obtain a current stack trace. The method Kernel.caller returns the current state of the call stack in the same form as
Exception.backtrace. caller returns a stack trace whose first element is the method that invoked the method that calls the caller without any argument. That is, caller[0] mentions the location from which the current method gets invoked. The caller can also be invoked with an argument that specifies the number of stack frames to drop from the start of the backtrace. The default being 1, and caller(0)[0] specifies the location at which the caller method gets invoked. 

Exception.backtrace and Kernel.caller return Stack trace. It includes method names. Before Ruby 1.9, the stack trace strings must be parsed to extract method names. In Ruby 1.9, however, the name, as a symbol of the currently executing method can be obtained with Kernel.__method__ or its synonym, Kernel.__callee__. __method__ is useful in conjunction with __LINE__ and __FILE__: 

raise "Assertion failed in #{__method__} at #{__FILE__}:#{__LINE__}" 
You can also try this code with Online Ruby Compiler
Run Code

 

Note :__method__ returns the name by which a method gets originally defined, even if the method gets invoked through an alias.  
 

Ruby allows tracing assignments to global variables with Kernel.trace_var. This method is passed as a symbol that names a string or block of code and global variable. Whenever the value of the named variable changes, the string gets evaluated or the block gets invoked. When a block is specified, the new value of the variable gets passed as an argument. To stop tracing the variable, Kernel.untrace_var is called. 

The last tracing method is Kernel.set_trace_func, which registers a Proc to be invoked after each line of a Ruby program. set_trace_func is useful in situations when a debugger module allows line-by-line stepping through a program written by the programmer.

Frequently Asked Questions 

Why do programmers perform tracing?

They do so to track the variable values when they change during execution and also to beforehand determine the output of the code. 

What is manual tracing in programming?

Manual code means that the programmer interprets the results of each line of code and keeps track by hand on paper.  

Conclusion

This article discusses Hooks in Ruby. You can read more about this language here
Further, if you have any doubts, you can refer to the official FAQ page of Ruby. 

To be an expert in Ruby, we recommend you to check out the Coding Ninjas Ruby Courses and Articles

Happy Coding!

Live masterclass