Ruby is an open-source and Object-oriented language invented by Yukihiro Matsumoto in the mid-90s. Ruby is a scripting language. A scripting language doesn't communicate with hardware directly, unlike languages like C and C++. After being written to a text file, an interpreter parses it and converts it to code. Typically procedural, these programs are read from top to bottom. On the other hand, object-oriented languages separate code into objects that may be made and used as needed. These items can be used again in different programs or even different apps.
Building desktop applications, static websites, data processing services, and even automation solutions is a breeze with Ruby. Web servers, DevOps, web scraping, and crawling are all used for it. Additionally, especially for database-driven online applications, you can accomplish much more when you use the power of the Rails application framework.
In this blog, we will study the Ruby language's top-level environment.
The Top-Level Environment:
Numerous classes, modules, constants, global variables, and global functions are defined and made accessible to applications by the Ruby interpreter upon startup. These preset characteristics are listed in the subsections that follow.
Predefined Modules and Classes
On starting upon ruby-interpreter 1.8, multiple classes and modules are defined. Some of these includes:
These classes and Modules can be directly used by the user when writing any applications without going into much detail about what's happening..
Top-Level Constants
Along with the previously mentioned modules and classes, certain top-level constants are also defined when the Ruby interpreter starts. By explicitly prefixing them with::, a module that declares a constant with the same name can still access these top-level constants. The top-level constants in your implementation can be listed using:
ARGF: An IO object that provides access to a virtual concatenation of the files mentioned in ARGV or, if ARGV is empty, standard input. A similar term to $.
ENV: An object that functions like a hash and gives users access to the interpreter's current environment variable settings.
FALSE: Deprecated Synonym used in place of False.
TRUE: Deprecated Synonym used in place of True.
Global Variables
Your applications can use several global variables that the Ruby interpreter has already defined. Many of these variables have unique properties. Some people's names contain punctuation. Some may not be allocated because they are read-only. Additionally, certain variables are thread-local, meaning they may have a different value in each thread of a Ruby application.
Finally, certain global variables have values exclusive to the current method, even if they are universally accessible. The value that the code that calls the method sees is unaffected if a method sets the value of one of these magic globals.
The full set of global variables that your Ruby interpreter has predefined is available by using:
ruby -e 'puts global_variables.sort'
Global settings
These global variables store configuration settings and define details about the environment in which the Ruby application is operating, such as command-line arguments:
$*
A read-only equivalent of the ARGV value. Synonym for English: $ARGV.
$$
Process ID for the current Ruby Process.
$?
The exit status of the last process was terminated.
$DEBUG, $-d
Sets true if -d or - - debug is mentioned on the command line.
There are more to this, but we have mentioned the important ones.
Exception-handling globals
When a rescue clause is used after an exception has been triggered, the next two global variables are helpful:
$!
This is used to get the last exception object raised.
$@
Stack trace of the Last exception.
Streams and text-processing globals
The IO streams and global variables impact how text-processing Kernel methods operate by default. Some of them include:
$stdin, $<
The standard input stream. The initial value is set to STDIN.
$stdout, $>
This corresponds to the standard output stream.
$FILENAME
The current file name which is read from ARGF.
$.
The last line received from the active input file, in numeric form.
Pattern-matching globals
Any Regexp pattern-matching operation sets the following thread-local and method-local global variables:
$+
The string that corresponds to the final group in the final pattern match that was successfully matched.
$'
The string that comes after the final pattern match.
$`
The string that came before the last pattern match.
Command-line option globals
Numerous global variables defined in Ruby are related to the status or value of interpreter command-line arguments. These include:
$-a
If the interpreter option -a was used, the value is true; otherwise, it is false.
$-l
if the -l option was used, true. Read-only
$-i
if the -i interpreter option wasn't used, nil. If -i is not supplied, the backup file extension is set for this variable.
Predefined Global Function
There are a number of private instance methods that act as global functions defined in the Kernel module, which is included by Object. Since they are private, they must be called functionally without specifying a receiving object. They may also be called anywhere since they are part of an Object; regardless of the value of self, it will be an object, and these methods can be called implicitly on it. These can be summoned in various categories.
Frequently Asked Questions
What is Ruby?
Yukihiro Matsumoto developed the object-oriented programming language Ruby, which is straightforward and effective. Ruby is adept at text processing, just like Perl. Similar to Smalltalk, Ruby treats Everything as an object and has blocks, iterators, meta-classes, and other useful features. Ruby may be used to create servers, test prototypes, and carry out routine programming activities. Ruby scales well because it is a fully integrated object-oriented language.
Does an object make a new copy when it is assigned?
Every variable and constant refers to (points at) a specific item. Uninitialized local variables, which have no references, are the exception. If used, these throw a NameError exception. The object is set when a variable or a constant is assigned a value.
As a result, an assignment by itself never makes a new duplicate of an object.
Can a class definition be repeated?
A class can have several definitions. Every definition is combined with the previous one. The previous technique is replaced and lost if it is redefined.
How are nil and false different from one another?
The only values in a boolean context that evaluate to false are nil and false.
However, they exhibit different behaviour elsewhere because they are instances of two distinct classes (NilClass and FalseClass). Predicate methods, whose names conclude with a question mark, are advised to only return true or false. Other methods that must return nil to signal failure should.
Conclusion
In this blog, we studied the Top Level environment in Ruby Language. Ruby is an open-source and Object-oriented programming language developed by Yukihiro Matsumoto. It is mostly used for scripting purpose, similar to Smalltalk. Furthermore, we saw various global functions which Ruby provides. These include various OS methods, Text manipulation functions, Conversion functions, etc. Once complete, you could refer to Everything you wished you knew about Ruby on Rails on the coding ninja's platform to learn more about Ruby.