Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Arrays in ruby
2.1.
Syntax
2.1.1.
Method1
2.1.2.
Method2
2.2.
Accessing elements
2.3.
Built in array methods
2.3.1.
.length
2.3.2.
.first and .last
2.3.3.
.take 
2.3.4.
.drop 
2.3.5.
.pop 
2.3.6.
.shift 
2.3.7.
.push 
2.3.8.
.unshift
2.3.9.
.delete 
2.3.10.
.reverse
3.
Frequently asked questions
3.1.
What makes Ruby different from other languages? 
3.2.
How can we access elements in an array?
3.3.
What is a literal constructor?
3.4.
What are the advantages of using Ruby?
4.
Conclusion
Last Updated: Mar 27, 2024

Arrays in Ruby

gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Ruby is one of the most used programming languages around the world because of the functionality it provides. Ruby is an interpreted, dynamic and purely object oriented language which was developed in 1995 by Yukihiro Matsumoto. The main idea behind the creation of ruby was to have language that can be typed in simple syntax to handle data and logic to solve problems.

Applications of Ruby

  • Desktop applications
  • Static websites
  • Data processing tools
  • Automation tools

Now after the brief introduction about ruby let's get to know about how we can implement arrays in ruby. We will learn about the syntax of arrays in ruby and operations we can apply on it.

Arrays in ruby

Ruby arrays are collections of any object that are ordered and integer-indexed. An index is assigned to each element in an array and is used to refer to it. String, Integer, Fixnum, Hash, Symbol, and even other Array objects can be stored in Ruby arrays. Arrays in Ruby aren't as rigid as those in other languages. While adding elements to a Ruby array, it grows automatically.

Arrays in Ruby can be created with more than one method and we will learn about them.

Syntax

Method1

Calling a .new() class method to create the array in Ruby with zero, one or more than one arguments.

#This syntax will intialize an array with the name of arraysname.
arraysname = Array.new()
#We can declare size with the new class method
arraysname = Array.new(10)
#We check the size of array with size and length method like
puts arraysname.size 
puts arraysname.length
#declaring the elements in array
arraysname = [12,32,"sam","jake",true]
#printing the array
puts arraysname

Output of the above code:

 

Method2

Using .literal constructor or square brackets [ ].

By using literal constructor there are more than one ways to intialize an array. 

Let’s see them:

#method1
Arrayname1 = [1,2,3,4,5,'first array' ] 
#method2
Arrayname2= Array.[](1,2,3,'second array')
#method3
Arrayname3 = Array[ 1,2,3,4,5,6,'third array']
# by using "#{ }" we can display the array elements in a block
puts "#{Arrayname1}"
puts "#{Arrayname2}"
puts "#{Arrayname3}"

Output of the above program:

Accessing elements

 

We can access the elements with indexing like arrayName[2] will return us the element at the position 2. We can also use negative indexing to access the elements like [-1] will return the element present at last in the array.

 

 

Time for an Example:

arrayname = [12,3,34,"sam","jake",[true,false]]
puts arrayname[1]
puts "#{arrayname[5]}"
puts arrayname[2,4]

#accessing the element more than size of array
puts arrayname[10] 

#can access the elemets with negative indexing
# by using "#{ }" we can display the array elements in a block
puts "#{arrayname[-1]}"
puts arrayname[-3]

Output:

Built in array methods

We are going to introduce a few built-in methods that are essential to know when you are working with ruby in order to make our work easier.

For all the methods we are going to use array: 

example = [12,3,34,4,5,6]

.length

.length method will return us the total length of the array by counting the elements.

puts example.length

Output => 5

.first and .last

.first will return us the first element  present in the array and .last will return us the last element in the array.

puts example.first
puts example.last

Output => 12

                 6

.take 

.take method will return us the first n elements in the array. n is the number will be given in the parameter in .take.

puts example.take(3)

This will return us the first three elements from the array.

Output => [12,3,34]

.drop 

.drop method will return us the elements in the array after the index n. n is the number will be given in the parameter in .take.

puts example.drop(3)

Output => [4,5,6]

.pop 

.pop method removes the last element from the array.

puts example.pop

Output => 6 

6 is the deleted element from the array

.shift 

.shift method removes the first element from the array.

puts example.shift

Output => 12

12 is the deleted element from the array

.push 

.push method adds an element to the end of the array.

puts example.push(9)

Output => [12,3,34,4,5,6,9]

.unshift

.unshift method adds an element at the beginning of the array.

puts example.unshift(0)

Output => [0,12,3,34,4,5,6]

.delete 

.delete method removes a specific element from the array.

puts example.delete(34)

Output => 34

34 is the deleted element from the array.

.reverse

.reverse method reverses the array elements but does not mutate it. So we have to declare another array to store the reversed array.

reversed = example.reverse
puts reversed

Output => [6,5,4,34,3,12] 

There are more built in methods available to us that we can apply in ruby like .each , .select, .include? and many more. You can explore them and use them and  practice these above built in methods so that you can use them more efficiently in your code.

Check out this problem - First And Last Occurrences Of X

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently asked questions

What makes Ruby different from other languages? 

Yukihiro Matsumoto designed Ruby, an object-oriented programming language that is both easy and powerful.

Ruby, like Perl, excels at text processing. Ruby, like Smalltalk, treats everything as an object, with blocks, iterators, meta-classes, and other useful features.

Ruby is a programming language that may be used to create servers, prototypes, and general programming tasks. Ruby scales nicely as a fully integrated object-oriented language.

How can we access elements in an array?

By indexing we can access elements in an array like array[2] will return the element present at the position 2.

What is a literal constructor?

[ ] or square bracket is called a literal constructor in ruby. It is use to intialize array in ruby.

For example: array = [ ]

What are the advantages of using Ruby?

  • It is an open source programming language.
  • It is easy to learn because of the easy to read syntax.
  • It is purely object oriented language.
  • It is a highly portable language.

 

Conclusion

In this article, we learned about Ruby language in general and how to use arrays in ruby like different methods to initialize it and how to access elements in array. Some built-in methods of arrays in ruby that we can use in our code.

To learn more about the Ruby language, please refer to the following article:

 8 reasons why ruby should be your first language

Ruby and ruby on rails: differences between them

Ruby on rails

 

Recommended problems -

 

To learn more about  DSA, competitive coding and many more knowledgeable topics please look into the guided paths on Coding Ninjas Studio. Also you can enroll into our courses and check out the mock test and problems available to you. Please check out our  interview experiences and interview bundle for placement preparations.

Please upvote our blog to help other ninjas grow.

Happy Learning

Previous article
Numbers in Ruby
Next article
Ruby Array Filter
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass