flatten ruby_Ruby中带有示例的Array.flatten方法

flatten ruby

Array.flatten方法 (Array.flatten Method)

In this article, we will study about Array.flatten method. You all must be thinking the method must be doing something related to flattening the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

在本文中,我们将研究Array.flatten方法 。 大家都必须认为该方法必须执行与展平Array实例有关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is one of the examples of the Public instance method which is specially defined in the Ruby library for Array class. This method is used to flatten the Array instances. This method works in the way that it returns a one-dimensional array by including all the Arrays present in the Array instance into self. This method can be destructive as well as non-destructive. Destructive methods are those methods that leave an impact on the Array elements or you can say that the changes made by them are permanent. Array.flatten! is the destructive version of this method whereas non-destructive methods do not create any change in the actual Array elements.

此方法是Ruby类库中为Array类专门定义的Public实例方法的示例之一。 此方法用于展平Array实例。 该方法通过将Array实例中存在的所有Array包含到self中来返回一维数组的方式工作。 此方法可以是破坏性的也可以是非破坏性的。 破坏性方法是那些会影响Array元素的方法,或者您可以说破坏性方法是永久的。 Array.flatten! 是此方法的破坏性版本,而非破坏性方法不会在实际的Array元素中产生任何变化。

Syntax:

句法:

    array_instance.flatten 
    or 
    array_instance.flatten!
    or 
    array_instance.flatten(level)

Argument(s) required:

所需参数:

In the type like last syntax, you need to pass the level of flattening. You will understand it with the help of programs.

在类似上一种语法的类型中,您需要传递展平级别。 您将在程序的帮助下了解它。

Example 1:

范例1:

=begin
  Ruby program to demonstrate flatten method
=end

# array declaration
array1 = ["Kumar",["Ramesh",["Apple","Pappu"],"Sana","Yogita"],"Satyam","Harish"]

puts "Array flatten implementation."
print array1.flatten

puts ""

puts "Array elements are:"
print array1

Output

输出量

Array flatten implementation.
["Kumar", "Ramesh", "Apple", "Pappu", "Sana", "Yogita", "Satyam", "Harish"]
Array elements are:
["Kumar", ["Ramesh", ["Apple", "Pappu"], "Sana", "Yogita"], "Satyam", "Harish"]

Explanation:

说明:

In the above code, you can see that all the sub-arrays are flattened and are stored in the main Array and their elements are now a direct part of the main Array. Since this is the non-destructive version, it is not creating any impact upon the actual Array elements.

在上面的代码中,您可以看到所有子数组都被展平并存储在主数组中,并且它们的元素现在是主数组的直接部分。 由于这是非破坏性版本,因此不会对实际的Array元素产生任何影响。

Example 2:

范例2:

=begin
  Ruby program to demonstrate flatten method
=end

# array declaration
array1 = ["Kumar",["Ramesh",["Apple","Pappu"],"Sana","Yogita"],"Satyam","Harish"]

puts "Array flatten implementation."
print array1.flatten!

puts ""

puts "Array elements are:"
print array1

Output

输出量

Array flatten implementation.
["Kumar", "Ramesh", "Apple", "Pappu", "Sana", "Yogita", "Satyam", "Harish"]
Array elements are:
["Kumar", "Ramesh", "Apple", "Pappu", "Sana", "Yogita", "Satyam", "Harish"]

Explanation:

说明:

In the above code, you can see that all the sub-arrays are flattened and are stored in the main Array and their elements are now a direct part of the main Array. Since this is the destructive version, it is creating an impact upon the actual Array elements.

在上面的代码中,您可以看到所有子数组都被展平并存储在主数组中,并且它们的元素现在是主数组的直接部分。 由于这是破坏性版本,因此会对实际的Array元素产生影响。

Example 3:

范例3:

=begin
  Ruby program to demonstrate flatten method
=end

# array declaration
array1 = ["Kumar",["Ramesh",["Apple","Pappu"],"Sana","Yogita"],"Satyam","Harish"]

puts "Array flatten implementation."
puts "Enter the level:"
level = gets.chomp.to_i

print array1.flatten(level)

puts ""

puts "Array elements are:"
print array1

Output

输出量

Array flatten implementation.
Enter the level:
 1
["Kumar", "Ramesh", ["Apple", "Pappu"], "Sana", "Yogita", "Satyam", "Harish"]
Array elements are:
["Kumar", ["Ramesh", ["Apple", "Pappu"], "Sana", "Yogita"], "Satyam", "Harish"]

Explanation:

说明:

In the above code, you can observe that we are asking the user about the level of flattening she wants in the Array instance. The actual Array is a 3-dimensional Array and after flattening by 1 level, now it can be considered as two-dimensional Array instance.

在上面的代码中,您可以观察到我们正在询问用户她想要在Array实例中进行展平的程度。 实际的Array是一个3维数组,在将其展平1级后,现在可以将其视为二维Array实例。

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

flatten ruby

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值