Category: Methods

  •  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…