Complete Tour Guide of Programming Language Ruby – Episode 4

Previously:

So, hello, lovely fellas, you have finally found the finale episode of the Ruby programming language series. In the previous episodes, we have covered basic concepts of the programming language, different types of operators and scopes with their suitable examples. So, in this final episode, we are going to study arrays, hash, and blocks of Ruby.

Arrays

Simply arrays can be referred to as the bags which contain numerous useful things. Arrays have comprised a group of different objects, numerous data types, all other stuff arranged in a particular sequence. The value of the arrays is always zero since the index value of the first item is zero. .each do is an iteration method that returns all elements of the array.

How to create an array: There are plenty of different methods present to create an array in the Ruby. But, today we will show you the most used method:

numerals = [14,15, 16, 17, 18]

fruits = %w( mango grape melon )

bills = Array.new

bills << 4.55 << 3.22 << 3.55 << 8.55 << 3.23

all_datatype = [1, -5,”text”, 4.3]

puts numerals.inspect

puts fruits.inspect

puts bills.inspect

puts all_datatype.inspect

In this example,  ‘%w’ will help you to add elements in an array without coma, whereas ‘inspect’ make you view the array object.

Hash

The hash in Ruby comprises pairs of important information. Each pair of information further comprises a name of a unique key and a value. In some cases, hashes are called the associated arrays. But, unlike arrays, hashes can be an arbitrary object like an index.

How to create a hash: To create a hash in the Ruby, users mainly have two methods- new keywords and hash literal. The created hash code in Ruby looks like this:

animals = Hash.new

#adding two pairs of values to the hash

animals[1] = “Dog”

animals[2] = “Cat”

animals[3]= “Rabbit”

puts animals

#Output : {1=>”Dog”, 2=>”Cat”, 3=>”Rabbit”}

P.S. In the above example, square brackets are used, but you can also use the store method for initializing the hash with the certain value.

Blocks

The blocks in Ruby can be expressed as the unidentified part of the code which can accept arguments and return a value. The blocks are very useful in passing the piece of code as an argument to a method using proc. Moreover, blocks are associated with the array to perform numerous operations. The syntax in the blocks held following command:

Single statement: ‘curly braces’

Multiple statements: ‘do and end’”

To better get look at the working of blocks, you can check out following example:

#multiple statements

do

puts “I am Ruby”

puts “greeting you from inside a block!”

end

#Single statement

{ puts “I am Ruby, greeting you from inside a block!” }

Some Exceptions

In the Ruby, you might face following exceptions also, so be prepared:

  • Ruby exceptional condition represent the object, that can be descendant or instance of the exceptional class.
  • The exception can be easily tackled by the user by providing the rescue clause. Whereas the program control will flow to the clause.
  • The Ruby has some predetermined classes, such as exception its children which are used to handle program errors.
  • Ruby has few built-in subclasses of exceptions like NoMemoryError, ScriptError, SecurityError, SignalException, StandardError, SystemExit, SystemStackError and fatal error.

So, folks that will be our wrap on the Ruby programming language series guide. We hope that you enjoyed our series and have gained something new from this series. So, don’t forget to share your thoughts with us as we always love to hear from you.

Send a Message