RUBY (part-6)

RUBY (part-6)

Introduction

I am writing this with utmost passion, which means that should I didn't do that in my previous blog. No, I didn't mean that because in programming everyone has some favorite concepts one like the linked list, another like graph for me arrays are the dearest part. Let's get started.

Why Array

Before going directly into the arrays let's understand why arrays are introduced. In the previous blog, we discussed all variables. They can store only one value at a time it is not possible to store more than one value. Let's imagine you decided to store the 5 values from 1 to 5. By convention, we first have to create 5 variables after this start assigning values to each element. Things get more sense when we look at this in the code format.

var_1 = 1
var_2 = 2
var_3 = 3
var_4 = 4
var_5 = 5

So, Guys as we are programmers we have to be lazy at any cost. The above code represents some redundancy in the code. Wouldn't it be super cool if we have a chance to store all the elements in a single variable? This is where ARRAY comes into the picture.

The best part of the values in the array would be of any type ( integers, strings, floating etc.,). Even the focal feature is it can allow us to store another array.

Array

In this context let's talk about the arrays officially. from the above stuff try to define what arrays mean.

Arrays are ordered, integer-indexed collections of any objects.

Try to break the above statement. To make the statement clear start understanding from the last.

collection of any objects --> In the why arrays concept we learned the same thing. It is allowing us to store more than one value of any type.

integer-indexed --> while storing the array signs some integer value to each object. Technically we can call it indices.

This is my way of understanding and definition of the array.

How

Let's look at how the array looks like? how we can store? how to get most of the arrays.

The simple form of array representation is by using square brackets "[]". we have to place the values that we want to store in between the square brackets.

Every element is associated with the index. It starts at 0 and ends at the length of the array-1. This means that 1st element is in index "0" the second element is at index "1" it goes on like this.

|---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 |  #This is how the array values associated with indices
|---+---+---+---+---+
 "0" "1" "2" "3" "4"

array = [1,2,3,4,5] #array with similiar datatypes
array = ["malavi", 1, 2.5] #array with different datatypes

Creating An Array

While learning about this I just literally shocked by those many ways. At one point I started asking myself by saying why they created these many. I not going to discuss all of them. As they are just ways to create an array there is no need to bother if we skip something. I am just focusing on only 2 or 3 ways.

  1. The most conventional way is with the new Array class method

syntax: array_name = Array.new[size]

note: placing size is optional. By using this we are simply telling the size of the array that we want.

array_name = Array.new #it is just creating the empty array
puts array_name
output:
[]
  1. The second one is also way most similar to the first one.

syntax: array_name = Array[Objects seperated by commas]

array_name = Array[1,2,3,4,5]
print array_name
output:
[1,2,3,4,5]
  1. This is the most simple way. It is the same as the python array declaration. As it is simple that's gonna be my favorite right?

syntax: array_name = [ objects separated by commas ]

array_name = [1,2,3,4,5]
print array_name
output:
[1,2,3,4,5]

Accessing

It is possible to access the particular array element using "[]" in tech terminology we can call them as subscript. For this, we first know about the index value of the element.

syntax: array_name[index_value]

That's as simple as that.

array_name = [1,2,3,4,5]
puts array_name[2] #print the second index value
output:
3

Array Methods

The best part of arrays in any programming language is it has numerous numbers of inbuilt methods to save a lot of developer time. Let's try to implement a few array methods.

A few array methods

length & size

The main function of these two methods is to return the array length/size. I don't know why we have 2 methods with similar working. If you know let me know.

as we only discussed the methods as the array is the class we can call them by using "." operator

a = [1,2,3,4,5]
puts a.length
output:
5

first & last

The first method returns the first element in the array and the last method returns the last element in the array.

a = [1,2,3,4,5]
puts a.first
puts a.last
output:
1
5

take & drop

For both methods we have to pass an argument it must be a positive integer. take method returns the first n elements of the array where as drop returns the last n elements in the array.

a = [1,2,3,4,5]
print a.take(3) #print the objects till the 3rd index
print a.drop(3) #print the objects start with index 3 till last
output:
[1,2,3] [4,5]

push & pop

push method takes one value as an argument it can be any data type and it adds the value at the end of the array. pop permanently removes the last element from the array.

a = [1,2,3,4,5]
print a.push(6)
puts a.pop
output:
[1,2,3,4,5,6] 6

shift & unshift

Both these methods are used for removing and adding an element at the starting index. shift removes the first element whereas unshift takes one value as an argument and adds it at starting of the array.

a = [1,2,3,4,5]
puts a.shift
print a.unshift(9)
output:
1
[9,2,3,4,5]

I am ending here dear readers it's not possible to discuss all of them. Do check them out in the link below.

All About Array Methods

Conclusion

That's all about this blog and I hope you learn something about arrays. Don't forget to try and get a hand on practice with all of them. Because while looking and reading it seems so simple but it's not gonna work out. Feel very free to share your experience in the comments I will be happier when commenting than HEARTS. See you in the next blog with Hashes.

Happy learning! Happy coding!!

Did you find this article valuable?

Support Malavi Pande by becoming a sponsor. Any amount is appreciated!