Category: 4. Ruby Core
-
Ruby Exceptions
Ruby exception is an object, an instance of the class Exception or descendent of that class. It represents some exceptional condition. In a Ruby program, when something goes wrong, it throws an exceptional behavior. By default Ruby program terminates on throwing an exception. We can declare some exception handlers within Ruby. An exception handler is…
-
Ruby Directories
Class Dir has directory streams as objects which represents directories in underlying file system. Directories are handled with Dir class. Creating a Directory To create a directory mkdir command is used. You can give permission to a directory if you want. Syntax: Example: We have created a directory “project” in out system. Checking a Directory…
-
Ruby Iterators
Iterator is a concept used in object-oriented language. Iteration means doing one thing many times like a loop. The loop method is the simplest iterator. They return all the elements from a collection, one after the other. Arrays and hashes come in the category of collection. Ruby Each Iterator The Ruby each iterator returns all…
-
Ruby Ranges
Ruby range represents a set of values with a beginning and an end. They can be constructed using s..e and s…e literals or with ::new. The ranges which has .. in them, run from beginning to end inclusively. The ranges which has … in them, run exclusively the end value. Output: Ruby has a variety of ways to define ranges.…
-
Ruby Hashes
A Ruby hash is a collection of unique keys and their values. They are similar to arrays but array use integer as an index and hash use any object type. They are also called associative arrays, dictionaries or maps. If a hash is accessed with a key that does not exist, the method will return…
-
Ruby Strings
Ruby string object holds and manipulates an arbitary sequence of bytes, typically representing characters. They are created using String::new or as literals. Quotes Ruby string literals are enclosed within single and double quotes. Example: Output: Accessing string elements You can access Ruby string elements in different parts with the help of square brackets []. Within square brackets…
-
Ruby Modules
Ruby module is a collection of methods and constants. A module method may be instance method or module method. Instance methods are methods in a class when module is included. Module methods may be called without creating an encapsulating object while instance methods may not. They are similar to classes as they hold a collection…
-
Ruby Blocks
Ruby code blocks are called closures in other programming languages. It consist of a group of codes which is always enclosed with braces or written between do..end. The braces syntax always have the higher precedence over the do..end syntax. Braces have high precedence and do has low precedence. A block is written in two ways, Both are…
-
Ruby Methods
Ruby methods prevent us from writing the same code in a program again and again. It is a set of expression that returns a value. Ruby methods are similar to the functions in other lnguages. They unite one or more repeatable statements into one single bundle. Defining Method To use a method, we need to…
-
Ruby Class and Object
Here, we will learn about Ruby objects and classes. In object-oriented programming language, we design programs using objects and classes. Object is a physical as well as logical entity whereas class is a logical entity only. Ruby Object Object is the default root of all Ruby objects. Ruby objects inherit from BasicObject (it is the parent class…