ruby中!!_Ruby反向! 功能
ruby中!!
逆轉(zhuǎn)! Ruby中的功能 (reverse! function in Ruby)
As the name suggests, reverse! function is used to reverse the elements of an array. Most of the times, we need to reverse an array but if we do it with the help of loops, the program will become quite lengthy and time-consuming. Ruby facilitates you with reverse! function which produces the reverse of the array. Unlike, reverse function in Ruby, reverse! produces changes in the original array itself. It also returns the reverse of the array which can be stored in the corresponding new array for the fulfillment of future operations.
顧名思義, 反向! 函數(shù)用于反轉(zhuǎn)數(shù)組的元素。 在大多數(shù)情況下,我們需要反轉(zhuǎn)數(shù)組,但是如果在循環(huán)的幫助下完成該程序,則該程序?qū)⒆兊孟喈斎唛L且耗時。 Ruby為您提供反向幫助! 函數(shù)產(chǎn)生與數(shù)組相反的結(jié)果。 與Ruby中的反向功能不同, 反向! 在原始數(shù)組本身中產(chǎn)生更改。 它還返回數(shù)組的倒序,可以將其存儲在相應(yīng)的新數(shù)組中,以實現(xiàn)將來的操作。
Syntax:
句法:
Array_name.reverse!Now, let us understand the implementation concept in a much broader way with the help of program codes.
現(xiàn)在,讓我們借助程序代碼以更廣泛的方式理解實現(xiàn)概念。
Example 1:
范例1:
=begin Ruby program to demonstrate implementation of reverse! function =end# Initializing some arrays of elements Arr1 = ["apple", "mango", "banana", "apricot", "plum", "cherry"] Arr2 = ["one","two", "three", "four", "five"] Arr3 = [10,20] Arr4 = [110, 210, 310, 410, 510] Arr5 = ["C++", "Java", "C#", "Visual Basic", "Perl", "Python"] Arr6 = ["Hrithik", "Satyam", "Amisha", "Kajal", "Wamp"] # Invoking reverse! function NewArr1 = Arr1.reverse! NewArr2 = Arr2.reverse! NewArr3 = Arr3.reverse! NewArr4 = Arr4.reverse! NewArr5 = Arr5.reverse! NewArr6 = Arr6.reverse!# Printing the the corresponding arrays puts "#{NewArr1}" puts "#{NewArr2}" puts "#{NewArr3}" puts "#{NewArr4}" puts "#{NewArr5}" puts "#{NewArr6}"Output
輸出量
["cherry", "plum", "apricot", "banana", "mango", "apple"] ["five", "four", "three", "two", "one"] [20, 10] [510, 410, 310, 210, 110] ["Python", "Perl", "Visual Basic", "C#", "Java", "C++"] ["Wamp", "Kajal", "Amisha", "Satyam", "Hrithik"]Code logic:
代碼邏輯:
In the above code, we have initialized six arrays. We are reversing them with the help of reverse! statement and storing the returned array in a new corresponding array. Eventually, we are printing the corresponding new arrays.
在上面的代碼中,我們已經(jīng)初始化了六個數(shù)組。 我們正在反轉(zhuǎn)的幫助下反轉(zhuǎn)它們! 語句并將返回的數(shù)組存儲在新的對應(yīng)數(shù)組中。 最終,我們將打印相應(yīng)的新數(shù)組。
Now, let us see verify whether reverse! function produces changes in the original array or not.
現(xiàn)在,讓我們看看是否可以逆轉(zhuǎn)! 函數(shù)是否在原始數(shù)組中產(chǎn)生更改。
Example 2:
范例2:
=begin Ruby program to demonstrate implementation of reverse! function =end# Initializing some arrays of elements Arr1 = ["apple", "mango", "banana", "apricot", "plum", "cherry"] Arr2 = ["one","two", "three", "four", "five"] Arr3 = [10,20] Arr4 = [110, 210, 310, 410, 510] Arr5 = ["C++", "Java", "C#", "Visual Basic", "Perl", "Python"] Arr6 = ["Hrithik", "Satyam", "Amisha", "Kajal", "Wamp"] # Invoking reverse! function NewArr1 = Arr1.reverse! NewArr2 = Arr2.reverse! NewArr3 = Arr3.reverse! NewArr4 = Arr4.reverse! NewArr5 = Arr5.reverse! NewArr6 = Arr6.reverse!# Printing the the corresponding arrays puts "#{Arr1}" puts "#{Arr2}" puts "#{Arr3}" puts "#{Arr4}" puts "#{Arr5}" puts "#{Arr6}"Output
輸出量
["cherry", "plum", "apricot", "banana", "mango", "apple"] ["five", "four", "three", "two", "one"] [20, 10] [510, 410, 310, 210, 110] ["Python", "Perl", "Visual Basic", "C#", "Java", "C++"] ["Wamp", "Kajal", "Amisha", "Satyam", "Hrithik"]Code Logic:
代碼邏輯:
In the above code, we have tried to show that reverse! function produces changes in the original array as well with the help of ‘puts’ statement through which we are printing the arrays.
在上面的代碼中,我們試圖證明這種相反! 函數(shù)會在原始數(shù)組中產(chǎn)生更改,并借助“ puts”語句來打印數(shù)組。
翻譯自: https://www.includehelp.com/ruby/reverse-1-function.aspx
ruby中!!
總結(jié)
以上是生活随笔為你收集整理的ruby中!!_Ruby反向! 功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nextdate函数白盒测试问题 软件测
- 下一篇: lcfirst_PHP lcfirst(