Arrays and hashes are indexed collections which are used to store a collection of objects and those stored objects are accessed via a key.

In case of arrays, the key is an integer while in case of hashes, the key is also an object. For example, in case of arrays, we can access the first element by array[0] while in hashes, we can access the element of key first by hash[first].

Array Creation
we can created and initialize array using array literal which is a set of elements between brackets.

Example: a = [1 , 'Cat' , 3.14]

In the above example, an array is defined using array literal, and it contains three different objects.

If we are defining an array of strings (words), then Ruby has a better way to do this using %w shortcut

Example: a = %w{cat dog ant}

Array Access
Array elements can be accessed by supplying index of the elements between brackets.

Example: a[1] -> ‘Cat’

Note: If we provided an index of non-existing element, we get nil

Hash Creation
Hashes are similar to arrays, they are created using hash literal. However, hashes are using braces instead of using square brackets.

Example:
inst_section = {
‘cello’ => ’string’,
‘clarinet’ => ‘woodwind’
}

Note:

  • The left hand side of the => represents the key, while the right hand side represents the value.
  • The keys of the hash should be unique.

Hash Access
In order to access a hash, we use the index key defined in the hash definition.

Example: inst_section['cello']

Note: If we try to access a hash using non-existing key, the returned value will be nil.


In this section, we provide some basic concepts of Ruby.

Defining a Method (Function)

Notes on defining function:

  • No need to use semi-colon at the end of the statement since each statement are in a separate line.
  • Ruby comments start with #
  • Indentation makes code more pretty.
  • functions are defined with def and ended with end.
  • using parentheses with function parameters is optional.
  • returning values from function is done using the keyword return.

Ruby String Object
String object in Ruby can be defined using two ways:

  • The first way is to use the single quotation marks.
  • The second way is to use the double quotation marks.

The difference between the two ways is in the amount of processing that Ruby performs on the string. In case of single-quoted string, Ruby performs very little processing.

In case of double-quoted string, Ruby process the string first before producing it. Ruby looks for substitutions, (which are things in strings that are substituted with other things). For example, escape sequences like \n are substituted with its equivalent, which is the new line.

Another substitution is is the sequence of #{expression} which is replaced with the value of expression. the expression can be the parameter sent to the function, or the output of a process performed on the parameter or any other formula.

A small note about the previous paragraph is that the braces in the #{expression} is not needed when using global, instance or class variables.

Note:

  • Global variable is prefixed with $ when defined. Example: $greeting = Hello
  • Instance variable is prefixed with @ when defined. Example: @name = Ahmed
  • When using global or instance variable in string, it’s used without braces as follows: “#$greeting #@name”

Naming Convention in Ruby

  • Local variables, method parameters, and method names should start with lowercase letters or underscore.
  • Global variables are prefixed with $.
  • Instance variables are prefixed with @.
  • Class variables are prefixed with @@.
  • Class names, module names, constants should start with uppercase letter.
  • Multi-word instance variables are written with underscore in between words.
  • Multi-word class names are written with MixedCase (i.e. each word starts with Uppercase letter)



So, I will assume that ruby is installed (not a complex process). I will go directly to the programming concepts.

Ruby as Object-Oriented Language
Ruby is a genuine object-oriented language. Everything is an object. results of manipulation are also objects.

Why object-oriented? Because using this programming models forces the developer to look from the Real-World point of view, and this is a good way to develop successful solutions.

Classes
In a typical software solution, the software consists of some entities that interconnects together. each entity has properties and functions, and those properties and functions are described in the class of the entity.

After describing a class, we can create the instance of this class, which is the object that has such properties and functions. We can create multiple objects, with different values of properties, and parameters passed to functions, but they have the same nature since they are of the same class.

Class Constructor
Objects are created by calling the constructor of the class, which is a function that exist in all classes and called by default when an object is created. the constructor usually contains initialization values for the object.

Instance Functions and Instance Variables
As we said before, the class contains description of object features, these features include both instance variables or instance functions.

Instance variables represent the states of the object, they are normal variables that describe some features of the object.

There are also instance functions, which are associated with each object and contains the various features the object can implement. Instance functions have an access over the instance variable.

Introduction

August 12, 2008

In this series of blogs, I’m summarizing the very powerful programming language Ruby. I’ve selected this language after I discovered that it’s powerful enough for many purposes, including web development, scripting and other general-purposes. So, Let’s discover it.