array.reverse_Ruby中带有示例的Array.reverse_each方法

array.reverse

Ruby Array.reverse_each方法 (Ruby Array.reverse_each Method)

In the last article, we have gone through the use of the Array.each method. You must remember that Array.each method is used to traverse the Array from the first element up to the last element and provides no mechanism to process the last element first and the last element in the very end.

在上一篇文章中,我们介绍了Array.each方法的使用。 您必须记住Array.each方法用于从第一个元素到最后一个元素遍历Array,并且没有提供任何机制来处理最后一个元素,最后一个元素。

To meet this problem with an appropriate solution, we have got Array.reverse_each method. This method, as the name suggests, reverses the element in a certain way. Since this method is non-destructive, this will never reverse the Array. It will only be used to traverse the Array in the reverse order which simply means that we have a facility to fetch the last element of Array instance first and the processing or operation will keep ongoing ever since the first element is not fetched.

为了通过适当的解决方案解决这个问题,我们有了Array.reverse_each方法 。 顾名思义,此方法以某种方式反转了元素。 由于此方法是非破坏性的,因此永远不会反转Array。 它仅用于以相反的顺序遍历Array,这仅意味着我们拥有一种设施,可以首先获取Array实例的最后一个元素,并且由于未获取第一个元素,因此处理或操作将一直进行。

Syntax:

句法:

    Array.each { |var| #statements}

Parameter(s):

参数:

This method does not invite any type of argument or arguments.

此方法不邀请任何类型的参数。

Example 1:

范例1:

=begin
  Ruby program to demonstrate Array.reverse_each
=end

# array declaration
Adc = ['Ruby','Includehelp.com','Ruby','C++','C#','Java','Python','C++','Java']

cnt = 1
Adc.reverse_each do |ele|
	puts "#{cnt} element is #{ele}"
	cnt = cnt + 1
end

Output

输出量

1 element is Java
2 element is C++
3 element is Python
4 element is Java
5 element is C#
6 element is C++
7 element is Ruby
8 element is Includehelp.com
9 element is Ruby

Example 2:

范例2:

=begin
  Ruby program to demonstrate Array.reverse_each
=end

# array declaration
Adc = ['Ruby','Includehelp.com','Ruby','C++','C#','Java','Python','C++','Java']

cnt = 1
Adc.reverse_each {|ele|
	puts "#{cnt} element is #{ele}"
	cnt = cnt + 1
}

Output

输出量

1 element is Java
2 element is C++
3 element is Python
4 element is Java
5 element is C#
6 element is C++
7 element is Ruby
8 element is Includehelp.com
9 element is Ruby

Explanation:

说明:

In the above two program codes, you can observe that the method named Array.reverse_each can be used in two different ways. Both the ways are quite understandable and their use depends upon your comfort. It is very clear with the help of output that the processing of Array elements starts from the last index and finishes when the first element is being processed. You can never bring any change in the elements of Array with the help of this method because this method is one of the examples of non-destructive methods. This method provides great help when the code requires the processing of the last method first. You can observe that in actual, "Java" is the last element but here it is represented as the very first element.

在以上两个程序代码中,您可以观察到可以以两种不同的方式使用名为Array.reverse_each的方法。 两种方式都是可以理解的,它们的使用取决于您的舒适度。 借助输出,很明显Array元素的处理从最后一个索引开始,并在处理第一个元素时结束。 借助于此方法,您永远无法对Array的元素进行任何更改,因为该方法是非破坏性方法的示例之一。 当代码需要首先处理最后一个方法时,此方法将提供很大的帮助。 您可以观察到,实际上, “ Java”是最后一个元素,但在这里它被表示为第一个元素。

翻译自: https://www.includehelp.com/ruby/array-reverse_each-method-with-example.aspx

array.reverse

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值