Ruby is a general-purpose, dynamic, reflective, object-oriented programming language. Everything in Ruby is an object. Ruby's development aimed to create a user interface between human programmers and the underlying computational machinery.
Global Variables and functions are an important part of any programming language.
Let’s learn about Global variables, global functions, and how to invoke global functions in Ruby.
Global Variables In Ruby
The scope of a Global Variable is global and can be accessed from anywhere in the program. At every point in the program, assigning global variables has global implications. The dollar sign ($) is always prefixed to global variables. We need to define a global variable if we want a single variable that is available across all classes. An uninitialized global variable has a nil value by default, and its use can make programs obscure and difficult. Anywhere in the program, a global variable can be changed.
Syntax:
$global_variable = 5
You can also try this code with Online Ruby Compiler
Let’s understand the declaration of global variables with a code example.
#Global Variable Declaration In Ruby
# global variable
$global_variable1 = 50
$global_variable2 = 100
# Defining class
class CodingNinjas1
def print_global_var
puts "Global variable in CodingNinjas1 is #$global_variable1"
end
end
# Defining Another Class
class CodingNinjas2
def print_global_var
puts "Global variable in CodingNinjas2 is #$global_variable2"
end
end
# Creating object
CodingNinjas1obj = CodingNinjas1.new
CodingNinjas1obj.print_global_var
# Creating another object
CodingNinjas2obj = CodingNinjas2.new
CodingNinjas2obj.print_global_var
You can also try this code with Online Ruby Compiler
We have declared two global variables and invoked them in two different classes, and we get the following output.
Global Functions In Ruby
Kernel-specified methods and any methods defined at the top-level, outside of any classes, are global functions. Global functions are defined as private methods of the Object class.
Note: Since global functions are methods of Object, they can be invoked (implicitly) from anywhere, irrespective of the value of self.
Kernel's global functions are used all over the place. In fact, it's likely that you're not even aware that you're using them.
Here are a few of them:-
1.) puts
2.) gets
3.) chomp
4.) sleep
5.) require
6.) gem
7.) rand
8.) gsub
9.) proc
10.) eval
Let’s understand how the Global functions are invoked with the help of an example.
Invoking Global Function - puts
As we have already discussed, puts is a private method Object class. It can't be called explicitly by anyone else, which means it can't be called by anyone else. But how come we use this method so frequently and with such ease? Because it's part of the Object class, it can be accessed by any Ruby object - but only in the current context. This explains why puts is never explicitly called on any object; instead, it is implicitly called on whatever self happens to be.
To put it another way, the method will be called on the current self context.
Using a method like puts in the current self context is also crucial. In fact, it's because of this that we can accomplish it in a Ruby class.
Let’s understand it with an example.
class Ninjas
def self.developer
puts "Ninjas are Developers!"
end
def coders
puts "Ninjas are Competitive Coders!!"
end
end
#Creating Object
obj = Ninjas.new
#Invoking Objects
Ninjas.developer
obj.coders
You can also try this code with Online Ruby Compiler
Even though the context of self changes from the Ninjas Class, to an instance of a Ninjas, we have access to puts in all contexts. And we get the output when it is invoked from anywhere in the code.
Note: Kernel's private instance methods are accessible almost anywhere in a Ruby program, regardless of self, because virtually all Ruby objects inherit from Object and have access to the methods provided in the Kernel module.
Frequently Asked Questions
What are Objects in Ruby?
In Ruby, everything is an object. All objects have a unique identification; they can also maintain a state and exhibit behavior in response to messages. Usually, these messages are sent out via method calls. A string is an example of a Ruby object.
What is a Class in Ruby?
In Ruby, classes are first-class objects, each being an instance of the class Class.
What is a Global Variable in Ruby?
The variable with a global scope and can be accessed from anywhere in the program is known as Global Variable.
What are Global Functions in Ruby?
Kernel-specified methods and any methods defined at the top-level, outside of any classes, are Global Functions. Global functions are defined as private methods of the Object class.
State an important property of Global Functions.
One of the most important properties of global functions is that as the global functions are methods of Object, they can be invoked (implicitly) from anywhere, irrespective of the value of self.
Conclusion
In this article, we have extensively discussed Global variables, global functions, and how to invoke global functions in Ruby with the help of code examples.