×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby Array.flatten Method

By IncludeHelp Last updated : December 01, 2024

In this article, we will study about Array.flatten method. You all must be thinking the method must be doing something related to flattening the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

Description and Usage

This method is one of the examples of the Public instance method which is specially defined in the Ruby library for Array class. This method is used to flatten the Array instances. This method works in the way that it returns a one-dimensional array by including all the Arrays present in the Array instance into self. This method can be destructive as well as non-destructive. Destructive methods are those methods that leave an impact on the Array elements or you can say that the changes made by them are permanent. Array.flatten! is the destructive version of this method whereas non-destructive methods do not create any change in the actual Array elements.

Syntax

array_instance.flatten 
or 
array_instance.flatten!
or 
array_instance.flatten(level)

Parameters

In the type like last syntax, you need to pass the level of flattening. You will understand it with the help of programs.

Example 1

=begin
  Ruby program to demonstrate flatten method
=end

# array declaration
array1 = ["Kumar",["Ramesh",["Apple","Pappu"],"Sana","Yogita"],"Satyam","Harish"]

puts "Array flatten implementation."
print array1.flatten

puts ""

puts "Array elements are:"
print array1

Output

Array flatten implementation.
["Kumar", "Ramesh", "Apple", "Pappu", "Sana", "Yogita", "Satyam", "Harish"]
Array elements are:
["Kumar", ["Ramesh", ["Apple", "Pappu"], "Sana", "Yogita"], "Satyam", "Harish"]

Explanation

In the above code, you can see that all the sub-arrays are flattened and are stored in the main Array and their elements are now a direct part of the main Array. Since this is the non-destructive version, it is not creating any impact upon the actual Array elements.

Example 2

=begin
  Ruby program to demonstrate flatten method
=end

# array declaration
array1 = ["Kumar",["Ramesh",["Apple","Pappu"],"Sana","Yogita"],"Satyam","Harish"]

puts "Array flatten implementation."
print array1.flatten!

puts ""

puts "Array elements are:"
print array1

Output

Array flatten implementation.
["Kumar", "Ramesh", "Apple", "Pappu", "Sana", "Yogita", "Satyam", "Harish"]
Array elements are:
["Kumar", "Ramesh", "Apple", "Pappu", "Sana", "Yogita", "Satyam", "Harish"]

Explanation

In the above code, you can see that all the sub-arrays are flattened and are stored in the main Array and their elements are now a direct part of the main Array. Since this is the destructive version, it is creating an impact upon the actual Array elements.

Example 3

=begin
  Ruby program to demonstrate flatten method
=end

# array declaration
array1 = ["Kumar",["Ramesh",["Apple","Pappu"],"Sana","Yogita"],"Satyam","Harish"]

puts "Array flatten implementation."
puts "Enter the level:"
level = gets.chomp.to_i

print array1.flatten(level)

puts ""

puts "Array elements are:"
print array1

Output

Array flatten implementation.
Enter the level:
 1
["Kumar", "Ramesh", ["Apple", "Pappu"], "Sana", "Yogita", "Satyam", "Harish"]
Array elements are:
["Kumar", ["Ramesh", ["Apple", "Pappu"], "Sana", "Yogita"], "Satyam", "Harish"]

Explanation

In the above code, you can observe that we are asking the user about the level of flattening she wants in the Array instance. The actual Array is a 3-dimensional Array and after flattening by 1 level, now it can be considered as two-dimensional Array instance.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.