Author: Saim Khalid
-
Iterators
Iterators are nothing but methods supported by collections. Objects that store a group of data members are called collections. In Ruby, arrays and hashes can be termed collections. Iterators return all the elements of a collection, one after the other. We will be discussing two iterators here, each and collect. Let’s look at these in detail. Ruby each Iterator…
-
Ranges
Ranges occur everywhere: January to December, 0 to 9, lines 50 through 67, and so on. Ruby supports ranges and allows us to use ranges in a variety of ways − Ranges as Sequences The first and perhaps the most natural use of ranges is to express a sequence. Sequences have a start point, an…
-
Date & Time
The Time class represents dates and times in Ruby. It is a thin layer over the system date and time functionality provided by the operating system. This class may be unable on your system to represent dates before 1970 or after 2038. This chapter makes you familiar with all the most wanted concepts of date and time.…
-
Hashes
A Hash is a collection of key-value pairs like this: “employee” = > “salary”. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. The order in which you traverse a hash by either key or value may seem arbitrary and will generally…
-
Arrays
Ruby arrays are ordered, integer-indexed collections of any object. Each element in an array is associated with and referred to by an index. Array indexing starts at 0, as in C or Java. A negative index is assumed relative to the end of the array — that is, an index of -1 indicates the last…
-
Strings
A String object in Ruby holds and manipulates an arbitrary sequence of one or more bytes, typically representing characters that represent human language. The simplest string literals are enclosed in single quotes (the apostrophe character). The text within the quote marks is the value of the string − ‘This is a simple Ruby string literal’…
-
Modules and Mixins
Modules are a way of grouping together methods, classes, and constants. Modules give you two major benefits. Modules define a namespace, a sandbox in which your methods and constants can play without having to worry about being stepped on by other methods and constants. Syntax module Identifier statement1 statement2 ……….. end Module constants are named…
-
Blocks
You have seen how Ruby defines methods where you can put number of statements and then you call that method. Similarly, Ruby has a concept of Block. Syntax block_name { statement1 statement2 ………. } Here, you will learn to invoke a block by using a simple yield statement. You will also learn to use a yield statement with parameters…
-
Methods
Ruby methods are very similar to functions in any other programming language. Ruby methods are used to bundle one or more repeatable statements into a single unit. Method names should begin with a lowercase letter. If you begin a method name with an uppercase letter, Ruby might think that it is a constant and hence…
-
Loops
Loops in Ruby are used to execute the same block of code a specified number of times. This chapter details all the loop statements supported by Ruby. Ruby while Statement Syntax while conditional [do] code end Executes code while conditional is true. A while loop’s conditional is separated from code by the reserved word do, a newline, backslash \, or a semicolon ;. Example #!/usr/bin/ruby$i…