Open In App

Ruby | Array rotate!() function

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

Array#rotate!() : rotate!() is a Array class method which returns self rotated in place so that the element at count comes first, and returns self.

Syntax: Array.rotate!()

Parameter: Array

Return: self rotated in place so that the element at count comes first, and returns self.

Example #1 :




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


Output :

rotate!() method form : [22, 33, nil, 5, 6, 18]

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

rotate!() method form : [6, 18, 22, 50]

Example #2 :




# Ruby code for rotate!() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
c = ["cat", nil]
  
# declaring array
b = ["cow", nil, "dog"]
  
# rotate! method example
puts "rotate!() method form : #{a.rotate!(1)}\n\n"
  
puts "rotate!() method form : #{b.rotate!(2)}\n\n"
  
puts "rotate!() method form : #{c.rotate!(-1)}\n\n"


Output :

rotate!() method form : ["nil", "dog", "abc"]

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

rotate!() method form : [nil, "cat"]



Similar Reads

Ruby | Array rotate() function
Array#rotate() : rotate() is a Array class method which returns a new array by rotating self so that the element at count is the first element of the new array. Syntax: Array.rotate() Parameter: Array Return: a new array by rotating self so that the element at count is the first element of the new array. Example #1 : # Ruby code for rotate() method
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
Ruby | Array class flatten!() function
flatten!() is a Array class method which returns the flattened the array and returns nil if there is no modification required Syntax: Array.flatten!() Parameter: Array Return: flattened array or nil if no modification is required Example #1 : # Ruby code for flatten!() method # declaring array a = [[18, 22], [ 33, nil, 5, 6]] # declaring array b =
1 min read
Ruby | Array class inspect() function
Inspect() is an Array class method which returns the all array elements. Syntax: Array.inspect() Parameter: Array Return: all array elements Example #1 : # Ruby code for inspect() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, nil, nil, 50, 6] # inspect puts "inspec
1 min read