在Ruby中用*运算符(new_array-> arr * int / string)创建数组实例

In the last articles, we have gone through the method by which we can create Array instance with the help of & operator. You all must know that in my articles that were related to Array creation are Public class methods and now in the upcoming articles, we will be learning about Public instance methods.

在上一篇文章中,我们介绍了可以在&运算符的帮助下创建Array实例的方法。 大家都必须知道,在与数组创建相关的文章中,它们是Public类方法,现在在以后的文章中,我们将学习Public实例方法。

Now, you all must be thinking about what is the difference between Public class methods and Public instance methods, then to clear your doubt, Public class methods can be invoked as soon as Ruby processes the execution of the class whereas Public instance methods can only be executed after the creation of an Instance of that particular class. We have studied methods like Array.new() which is a resident of Public class methods but now will be studying Public instance methods. Well, in this article, we will be studying about creating the Array instance with the help of * operator.

现在,您必须都在考虑Public类方法和Public实例方法之间的区别,然后为澄清您的疑问,只要Ruby处理类的执行,就可以调用Public类方法,而Public实例方法只能是在创建该特定类的实例之后执行。 我们研究了Array.new()之类的方法,该方法是Public类方法的居民,但现在将研究Public实例方法。 好吧,在本文中,我们将研究如何在*运算符的帮助下创建Array实例

Method description:

方法说明:

The * symbol is the way to create an Array object and it requires one previously defined Array instance. It is also known as Set repetition. This can be an alias for Array.join(Arr1). It works in the way that it returns a new Array instance having elements after joining the elements of the Array with the integer or string passed as the argument in the method. The order is maintained from the original Array.

*符号是创建Array对象的方法,它需要一个先前定义的Array实例。 也称为“ 设置重复” 。 这可以是Array.join(Arr1)的别名。 它的工作方式是,在将Array的元素与方法中作为参数传递的整数或字符串连接起来之后,返回具有元素的新Array实例。 该顺序是从原始Array维护的。

Syntax:

句法:

    new_array = old_array * int/string

Parameter(s):

参数:

The '*' operator takes two arguments. The first one is the previously defined Array and the second one is String or integer.

“ *”运算符采用两个参数。 第一个是先前定义的数组,第二个是String或整数。

Example 1:

范例1:

=begin
  Ruby program to create Array with * operator 
=end

# arrays
old_arr1 = [23,44,66]

# creating a new array 
new_arr = old_arr1 * 4

# printing the array
puts "The new Integer Array Instance is:"
print new_arr

Output

输出量

The new Integer Array Instance is:
[23, 44, 66, 23, 44, 66, 23, 44, 66, 23, 44, 66]

Explanation:

说明:

In the above code, you can observe that we are creating an Array instance with the help of * operator. In resulting Array, you can see that our elements are repeated 4 times along with an additional four with the Array elements and we are providing that 4 as the argument to the method.

在上面的代码中,您可以观察到我们正在*运算符的帮助下创建一个Array实例 。 在结果Array中,您可以看到我们的元素重复了4次,另外四个元素与Array元素重复了,我们将这4个作为方法的参数提供。

Example 2:

范例2:

=begin
  Ruby program to create Array with * operator 
=end

# array
old_arr = ['Ramit','Amit','Suresh','Payal','Samir','Sonakshi','Hira','Panna']

# creating a new array
new_arr = old_arr * ","

# printing the array
puts "The new String Array Instance is:"
print new_arr

Output

输出量

The new String Array Instance is:
Ramit,Amit,Suresh,Payal,Samir,Sonakshi,Hira,Panna

Explanation:

说明:

In the above code, you can observe that we are creating a String Array with the help of * operator. The character which we are providing is "," and it is getting integrated after every element of the Array.

在上面的代码中,您可以观察到我们正在使用*运算符创建一个字符串数组 。 我们提供的字符是“,”,并且在数组的每个元素之后都将其集成。

翻译自: https://www.includehelp.com/ruby/creating-array-instance-with-asterisk-operator-new_array-arr-int-string.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值