ruby_Ruby反向功能

ruby

Ruby中的反向功能 (reverse function in Ruby)

In ruby, as the name suggests, reverse function is used to reverse the array but with a twist, it reverses the array and stores it into the new array. The reverse function produces no effect on the original array. By this function, whatever the order of the array is, that order gets reversed.

顾名思义,在ruby中, reverse函数用于反转数组,但要稍加扭转,它将反转数组并将其存储到新数组中。 反向函数对原始数组不起作用。 通过此函数,无论数组的顺序是什么,该顺序都可以颠倒。

The reverse function does not permit any parameters. Its return type is an array and obviously, the array returned is the reverse of actual array.

反向功能不允许使用任何参数。 它的返回类型是一个数组,显然,返回的数组与实际数组相反。

Syntax:

句法:

 new_array = old_array.reverse

Now, let us understand the implementation of the reverse function more broadly with the help of examples.

现在,让我们借助示例更广泛地了解反向函数的实现。

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"] 

# Calling to reverse function 
NewArr1 = Arr1.reverse 
NewArr2 = Arr2.reverse 
NewArr3 = Arr3.reverse 
NewArr4 = Arr4.reverse 
NewArr5 = Arr5.reverse 
NewArr6 = Arr6.reverse

# Printing the new reversed array 
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"]

In the above example, you can observe that we have initialized six arrays and then we are invoking reverse function inside the new array. reverse function is returning the array in reverse order and storing the values in a new corresponding array.

在上面的示例中,您可以观察到我们已经初始化了六个数组,然后在新数组中调用了反向函数。 reverse函数以相反的顺序返回数组并将值存储在新的对应数组中。

Now let us find, whether changes made by the reverse function are reflected in the original array or not by taking reference of an example given below,

现在,让我们通过参考下面给出的示例来查找由反向函数所做的更改是否反映在原始数组中,

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"] 

# Calling to reverse function 
NewArr1 = Arr1.reverse 
NewArr2 = Arr2.reverse 
NewArr3 = Arr3.reverse 
NewArr4 = Arr4.reverse 
NewArr5 = Arr5.reverse 
NewArr6 = Arr6.reverse

# Printing the original array 
puts "#{Arr1}"
puts "#{Arr2}"
puts "#{Arr3}"
puts "#{Arr4}"
puts "#{Arr5}"
puts "#{Arr6}"

Output

输出量

["apple", "mango", "banana", "apricot", "plum", "cherry"]
["one", "two", "three", "four", "five"]
[10, 20]
[110, 210, 310, 410, 510]
["C++", "Java", "C#", "Visual Basic", "Perl", "Python"]
["Hrithik", "Satyam", "Amisha", "Kajal", "Wamp"]

You can observe the difference between both the outputs. With the help of an example, it is practically proven that reverse function does not affect the original array.

您可以观察到两个输出之间的差异。 借助示例,实践证明了反向功能不会影响原始数组。

翻译自: https://www.includehelp.com/ruby/reverse-function.aspx

ruby

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值