Ruby | Array cycle() operation Last Updated : 08 Jan, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Array#cycle() : cycle() is a Array class method which returns the array by calling the given block each time for every element in the array 'n' no. of times and if 'nil' is given, then it will call it for forever. Syntax: Array.cycle() Parameter: block - condition n - number of times to call the block Return: array by calling the given block each time for every element in the array 'n' no. of times Code #1 : Example for cycle() method Ruby # Ruby code for cycle() method # declaring array a = [18, 22, 33, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, nil, nil, 50, 6] # cycling the array elements puts "cycle : #{a.cycle(3){ |x| puts x*x }}\n\n" # cycling the array elements puts "cycle : #{b.cycle(2){|x| puts x}}\n\n" Output : 324 484 1089 25 36 324 484 1089 25 36 324 484 1089 25 36 cycle : 1 4 1 1 88 9 1 4 1 1 88 9 cycle : Code #2 : Example for cycle() method Ruby # Ruby code for cycle() method # declaring array a = ["abc", "nil", "dog"] # declaring array b = ["cow", "1", "dog"] # cycling the array elements puts "cycle : #{a.cycle(3){ |x| puts x }}\n\n" # cycling the array elements # passing negative value for cycle puts "cycle : #{b.cycle(-1){|x| puts x}}\n\n" Output : abc nil dog abc nil dog abc nil dog cycle : cycle : Comment More infoAdvertise with us Next Article Ruby | Array collect!() operation M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Collections Ruby Array-class Similar Reads Ruby | Array clear() operation Array#clear() : clear() is an Array class method which removes all the array elements from it. Syntax: Array.clear() Parameter: Arrays to clear off Return: array with all elements cleared Code #1 : Example for clear() method Ruby # Ruby code for clear() method # declaring array a = [1, 2, 3, 4] # de 1 min read Ruby | Array collect() operation Array#collect() : collect() is an Array class method which invokes the argument block once for each element of the array. A new array is returned which has the value returned by the block. Syntax: Array.collect() Parameter: Arrays in which we want elements to be invoked Return: array with all the en 1 min read Ruby | Array collect!() operation Array#collect!() : collect!() is an Array class method which invokes the argument block once for each element of the array, replacing the element with the value returned by the block Syntax: Array.collect!() Parameter: Arrays in which we want elements to be invoked Return: array with all the envoked 1 min read Ruby | Array delete() operation Array#delete() : delete() is a Array class method which returns the array after deleting the mentioned elements. It can also delete a particular element in the array. Syntax: Array.delete() Parameter: obj - specific element to delete Return: last deleted values from the array. Code #1 : Example for 2 min read Ruby | Array at() operation Array#at() : at() is an Array class method which returns the element at the specific argumented index value. Syntax: Array.at() Parameter: - Arrays to search elements. - index to search Return: Array element at a specific index value Code #1 : Example for at() method Ruby # Ruby code for at() method 1 min read Ruby | Array class eql?() operation Array#eql?() : eql?() is a Array class method which checks if the two arrays are equal or not. Syntax: Array.eql?() Parameter: Arrays to compare Return: true - if the arrays are equal; otherwise false Code #1 : Example for eql?() method Ruby # Ruby code for eql?() method # declaring array a = [18, 2 1 min read Like