*args和**args
In the previous articles, we have learned some of the methods available in the Ruby library specially defined and declared for instances of Array class. You can also notice that in the program codes written to demonstrate all those methods are having Array instances declared with the conventional method such as,
在之前的文章中,我们学习了Ruby库中为Array类的实例专门定义和声明的一些可用方法。 您还可以注意到,在编写用于演示所有这些方法的程序代码中,它们具有使用常规方法声明的Array实例,例如,
array_name = ['ele1', 'ele2', 'ele3', ..., 'eleN']
The above is the way we have used so far in our articles to declare an array. In the upcoming articles, you will learn the different ways through which we can declare an array instance. Well, in this article, we will see how we can declare an Array object with the help of [](*args)?
以上是我们到目前为止在文章中用来声明数组的方式 。 在接下来的文章中,您将学习声明数组实例的不同方法。 好吧,在本文中,我们将看到如何在[](* args)的帮助下声明Array对象?
Method description:
方法说明:
This method is a public class method. You will have to provide the elements inside the round brackets and it will return an Array instance populated with the elements provided by you at the time of declaration. This method is one of the easiest and important methods for Array declaration.
此方法是公共类方法。 您将必须在圆括号内提供元素,并且它将返回一个Array实例,该实例填充有您在声明时提供的元素。 此方法是用于数组声明的最简单且重要的方法之一。
Syntax:
句法:
array_name = Array.[]('ele1', 'ele2' , ... ,'eleN')
Parameter(s):
参数:
Arguments play a very important role in this method. If you don't provide any arguments, it will result in an empty Array with no elements in it. This method can take any number of arguments.
在这种方法中,参数起着非常重要的作用。 如果不提供任何参数,它将导致一个空数组,其中没有任何元素。 此方法可以使用任意数量的参数。
Example 1:
范例1:
=begin
Ruby program to demonstrate Array.[](*args)
=end
# array declaration
arr = Array.[](1,"Sameer","Graphic Era")
# printing array elements
puts "Elements of \'arr\' are:"
puts arr
# empty array declaration
arr1 = Array.[]()
puts "Number of elements present in \'arr1\' are: #{arr1.count}"
Output
输出量
Elements of 'arr' are:
1
Sameer
Graphic Era
Number of elements present in 'arr1' are: 0
Explanation:
说明:
With the help of the above code, you can easily understand the implementation of Array.[] method. You can also observe that if we are not giving parameters to the method, then the Array results into an empty Array and we have shown that with the help of Array.count method.
借助以上代码,您可以轻松了解Array。[]方法的实现 。 您还可以观察到,如果我们没有为该方法提供参数,则Array会生成一个空Array,并且借助Array.count method可以证明这一点。
Example 2:
范例2:
=begin
Ruby program to demonstrate Array.[](*args)
=end
# input array elements
puts "Enter the first element you want to keep in the Array instance"
ele1 = gets.chomp
puts "Enter the second element you want to keep in the Array instance"
ele2 = gets.chomp
puts "Enter the third element you want to keep in the Array instance"
ele3 = gets.chomp
puts "Enter the fourth element you want to keep in the Array instance"
ele4 = gets.chomp
# creating array using input elements
arr = Array.[](ele1,ele2,ele3,ele4)
# printing array elements
puts "Array elements are:"
puts arr
Output
输出量
Enter the first element you want to keep in the Array instance
Hrithik
Enter the second element you want to keep in the Array instance
B.Tech
Enter the third element you want to keep in the Array instance
Graphic Era University
Enter the fourth element you want to keep in the Array instance
India
Array elements are:
Hrithik
B.Tech
Graphic Era University
India
Explanation:
说明:
In the above code, you can observe that we are taking multiple inputs from the user and we are passing those inputs as the elements which will be stored in the Array instance.
在上面的代码中,您可以看到我们从用户那里获取了多个输入,并且将这些输入作为元素存储在Array实例中。
翻译自: https://www.includehelp.com/ruby/creating-array-with-array-args.aspx
*args和**args