Home » Ruby programming

Array.map and Array.map! Methods with Example in Ruby

Ruby Array.map and Array.map! Methods: Here, we are going to learn about the Array.map and Array.map! Methods with Example in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on December 22, 2019

Ruby Array.map and Array.map! Methods

In the last article, we have studied about Array.reverse_each method. This method is used to print the Array in reverse order without bringing any changes in the value of elements stored in the Array instance. Array.each also works in the same manner and a type of non-destructive method. But we need a way through which we can bring some changes in the Element values or we can bring some changes in the value present in the Array.

Array.map method is meant for this purpose. It is used to generate a new array with the help of the present array with some changes which are supplied in the block. Array.map is a non-destructive method which simply means that it will not affect the actual Array whereas if you want to bring changes in the actual Array as well, you can introduce Array.map! method in your code.

Syntax:

    Array.map { |var| #statements}
    
    # or
    
    Array.map! { |var| #statements}
    
    # or
    
    Array.map do |var| 
        #statements
    end
    
    # or
    
    Array.map! do |var| 
        #statements
    end

Parameter(s):

This method does not take any type or any number of parameters.

Example 1:

=begin
  Ruby program to demonstrate Array.map
=end

# array declaration
Adc = ['Ruby','Includehelp.com','Ruby','C++','C#','Java','Python','C++','Java']

puts Adc.map {|ele|
	ele = ele + " is a language"
}

puts "Actual elements are:"
print Adc

Output

Ruby is a language
Includehelp.com is a language
Ruby is a language
C++ is a language
C# is a language
Java is a language
Python is a language
C++ is a language
Java is a language
Actual elements are:
["Ruby", "Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++", "Java"]

Explanation:

In the above code, you can observe that Array.map is not bringing any change in the actual Array element values. We are concatenating String "is a language" with every element present in the Array. The above code can also be modified with the help of do- end block. When we are printing the Array, we are getting the elements with their actual values.

Example 2:

=begin
Ruby program to demonstrate Array.map!
=end

# array declaration
Adc = ['Ruby','Includehelp.com','Ruby','C++','C#','Java','Python','C++','Java']

cnt = 1
puts Adc.map! {|ele|
	ele = ele + " is a language"
}

puts "Actual elements are:"
puts Adc

Output

Ruby is a language
Includehelp.com is a language
Ruby is a language
C++ is a language
C# is a language
Java is a language
Python is a language
C++ is a language
Java is a language
Actual elements are:
Ruby is a language
Includehelp.com is a language
Ruby is a language
C++ is a language
C# is a language
Java is a language
Python is a language
C++ is a language
Java is a language

Explanation:

In the above code, you can observe that Array.map! is bringing change in the actual Array elements. This is a very fruitful method when you want to update the actual values of the element present in the Array. Alike Array.map, this method can also be modified with the help of a do-end block.



Comments and Discussions!

Load comments ↻





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