Open In App

Ruby | Array replace() function

Last Updated : 06 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Array#replace() : replace() is a Array class method which returns an array of all combinations of elements from all arrays.

Syntax: Array.replace()

Parameter: Array

Return: an array of all combinations of elements from all arrays.

Example #1 :




# Ruby code for replace() method
  
# declaring array
a = [18, 22, 33, nil, 5, 6]
  
# declaring array
b = [1, 4, 1, 1, 88, 9]
  
# declaring array
c = [2, 5]
  
# replace method example
a.replace(b)
puts "replace() method form : #{a}\n\n"
  
b.replace(c)
puts "replace() method form : #{b}\n\n"
  
c.replace(a)
puts "replace() method form : #{c}\n\n"


Output :

replace() method form : [1, 4, 1, 1, 88, 9]

replace() method form : [2, 5]

replace() method form : [1, 4, 1, 1, 88, 9]


Example #2 :




# Ruby code for replace() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
c = ["hi"]
  
# declaring array
b = ["cow", nil, "dog"]
  
# replace method example
a.replace(b)
puts "replace() method form : #{a}\n\n"
  
b.replace(c)
puts "replace() method form : #{b}\n\n"
  
c.replace(a)
puts "replace() method form : #{c}\n\n"


Output :

replace() method form : ["cow", nil, "dog"]

replace() method form : ["hi"]

replace() method form : ["cow", nil, "dog"]



Similar Reads

Ruby | Set replace() function
The replace() is an inbuilt method in Ruby which replaces the contents of the set with the contents of the given enumerable object and returns self. Syntax: s1.replace(enum) Parameters: The function accepts an enumerable object which to be replaced in the set. Return Value: It returns the self object which contains the contents of the set after rep
1 min read
Ruby | Hash replace() method
Hash#replace() : replace() is a Hash class method which replaces the content of one hash with other. Syntax: Hash.replace() Parameter: Hash values Return: replaces the content of one hash with other. Example #1 : # Ruby code for Hash.replace() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, b:200} # declar
2 min read
Ruby Search and Replace
sub and gsub string methods that use regular expressions, and their in-place variants are sub! and gsub!. The sub & sub! replaces the first occurrence of the pattern and gsub & gsub! replaces all occurrences. All of these methods perform a search-and-replace operation using a Regexp pattern. sub! and gsub! modify the string on which they are called
2 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Ruby | Array Concatenation using (+) function
Array#+() is a Array class method which performs set concatenate operation arrays by combining two arrays to a third array. Syntax: Array.+() Parameter: Arrays for performing the concatenation operation. Return: New arrays by combining two arrays. Example #1 : # Ruby code for +() method # showing concatenate operation # declaring array a = [18, 22,
2 min read
Ruby | Array assoc() function
The assoc() function in Ruby is used to search through an array of arrays whose first element is compared with the index of the function and return the contained array if match found otherwise return either nil or vacant. Syntax: Array.assoc(Object) Here Array is the array of arrays. Parameters: Object : It is an element which gets compared with th
2 min read
Ruby | Array abbrev() function
abbrev() is an Array class method which provides unambiguous set of abbreviations for the string. Syntax: Array.abbrev()Parameter:- string for abbreviation - pattern [optional]Return: unambiguous abbreviations set Example #1 : Example for abbrev() method C/C++ Code # Ruby code for abbrev() method # checking for unambiguous abbreviations require 'ab
1 min read
Ruby Array () function with example
Array#() : () is an Array class method which performs the comparison between the two arrays. Syntax: Array.() Parameter: Array for the comparison Return: -1 : array is less than the other array 0 : array is equal to other array 1 : array is greater than the other array Example #1 : # Ruby code for <=>() method # showing comparison # declaring
2 min read
Ruby | Array class first() function
first() is a Array class method which returns the first element of the array or the first 'n' elements from the array. Syntax: Array.first() Parameter: Array n - no. of elements Return: first element of the array or the first 'n' elements from the array Example #1 : # Ruby code for first() method # declaring array a = [18, 22, 33, nil, 5, 6] # decl
1 min read
Ruby | Array class flatten() function
flatten() is an Array class method which returns flattened array i.e. a 1D array Syntax: Array.flatten() Parameter: Array Return: 1D array Example #1 : # Ruby code for flatten() method # declaring array a = [[18, 22], [ 33, nil, 5, 6]] # declaring array b = [[[1, 4, 1, 1, 88, 9]]] # flatten puts "flatten : #{a.flatten()}\n\n" # flatten pu
1 min read