欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ruby中带有示例的Array.collect方法

程序员文章站 2024-02-14 17:15:34
...

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

In previous articles, we have seen how we can implement Array.assoc() and Array.include?() methods in our Ruby code. Both the methods were used for finding the presence of certain objects in an object of Array class. Both the methods had certain differences and we have gone through them as well. In the article, we will see how we can use Array.collect method in the Ruby program. We will implement the method with the help of its syntax and program codes.

在先前的文章中,我们已经看到了如何在Ruby代码中实现Array.assoc()Array.include?()方法。 两种方法都用于查找Array类对象中某些对象的存在。 两种方法都有一定的差异,我们也对它们进行了介绍。 在本文中,我们将看到如何在Ruby程序中使用Array.collect方法 。 我们将借助其语法和程序代码来实现该方法。

Method description:

方法说明:

This method comes under the category of public instance method and is defined for the Array class in the library of Ruby language. This method traverses through the Array instance and creates modifications according to the requirement of the Ruby code. This method can be destructive as well as non-destructive which means that if the method is destructive then the modifications done by this method will be reflected in the actual Array instance, you can make the method destructive by adding a "!" mark after the method. As discussed above, this method can be non-destructive as well which simply means that the changes created by this method would not affect the actual Array instance.

此方法属于公共实例方法的类别,并且是在Ruby语言库中为Array类定义的。 该方法遍历Array实例并根据Ruby代码的要求创建修改。 此方法可以是破坏性的,也可以是非破坏性的,这意味着如果该方法是破坏性的,则此方法所做的修改将反映在实际的Array实例中,您可以通过添加“!”使方法具有破坏性 方法后标记。 如上所述,该方法也可以是非破坏性的,这仅意味着该方法创建的更改不会影响实际的Array实例。

Syntax:

句法:

    array_instance.collect {#block}

Example 1:

范例1:

=begin
	Ruby program to demonstrate collect method
=end

# array declaration
array1 = ["1","Ramesh","Apple","12","Sana","Yogita","Satyam","Harish"]

# input
puts "Enter the character you want to add"
ele = gets.chomp

puts "Array instance after modification:"
print array1.collect { |x| x + ele } 

puts ""

puts "Actual Array:"
puts array1     

Output

输出量

Enter the character you want to add
 ^
Array instance after modification:
["1^", "Ramesh^", "Apple^", "12^", "Sana^", "Yogita^", "Satyam^", "Harish^"]
Actual Array:
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish

Explanation:

说明:

In the above code, you can observe that this variant of the Array.collect method is a non-destructive one as the changes made by the method cannot be seen in the actual Array instance when we are printing it with the help of puts statement.

在上面的代码中,您可以观察到Array.collect方法的此变体是非破坏性的,因为在使用puts语句进行打印时,在实际的Array实例中看不到该方法所做的更改。

Example 2:

范例2:

=begin
	Ruby program to demonstrate collect! method
=end

# array declaration
array1 = ["1","Ramesh","Apple","12","Sana","Yogita","Satyam","Harish"]

# input
puts "Enter the character you want to add"
ele = gets.chomp

puts "Array instance after modification:"
print array1.collect! { |x| x + ele } 

puts ""

puts "Actual Array:"
puts array1        

Output

输出量

Enter the character you want to add
 %
Array instance after modification:
["1%", "Ramesh%", "Apple%", "12%", "Sana%", "Yogita%", "Satyam%", "Harish%"]
Actual Array:
1%
Ramesh%
Apple%
12%
Sana%
Yogita%
Satyam%
Harish%

Explanation:

说明:

In the above code, you can observe that this variant of Array.collect method is a destructive one as the changes made by the method can be seen in the actual Array. You can see that in both the statements we are getting an additional "%" symbol with all the elements present in the Array instance. This example simply tells you how you can create the method destructive with the help of "!" symbol.

在上面的代码中,您可以观察到Array.collect方法的此变体是破坏性的,因为在实际的Array中可以看到该方法所做的更改。 您可以看到,在这两个语句中,我们都获得了一个附加的“%”符号,其中的所有元素都存在于Array实例中。 本示例仅告诉您如何使用“!”创建破坏性方法 符号。

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