ruby中!!_Ruby反向! 功能

ruby中!!

逆转! 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.

顾名思义, 反向! 函数用于反转数组的元素。 在大多数情况下,我们需要反转数组,但是如果在循环的帮助下完成该程序,则该程序将变得相当冗长且耗时。 Ruby为您提供反向帮助! 函数产生与数组相反的结果。 与Ruby中的反向功能不同, 反向! 在原始数组本身中产生更改。 它还返回数组的倒序,可以将其存储在相应的新数组中,以实现将来的操作。

Syntax:

句法:

 Array_name.reverse!

Now, let us understand the implementation concept in a much broader way with the help of program codes.

现在,让我们借助程序代码以更广泛的方式理解实现概念。

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.

在上面的代码中,我们已经初始化了六个数组。 我们正在反转的帮助下反转它们 语句并将返回的数组存储在新的对应数组中。 最终,我们将打印相应的新数组。

Now, let us see verify whether reverse! function produces changes in the original array or not.

现在,让我们看看是否可以逆转! 函数是否在原始数组中产生更改。

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.

在上面的代码中,我们试图证明这种相反! 函数会在原始数组中产生更改,并借助“ puts”语句来打印数组。

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

ruby中!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值