ruby三元操作符_在Ruby中使用<<操作符将元素添加到数组实例中

ruby三元操作符

In the previous articles, we have gone through ways through which we can create Array instances. Some of them were Public instance methods and some were Public class methods. We should also know how they both differ from each other. Now we know multiple ways through which we can declare or generate our Array instances. Some are direct bypassing some arguments and some with the help of previously defined Array objects. Now, we will learn how we can add some elements to the previously defined Array? In this article, we will be learning about << with the help of which we can add elements to the instances of Array class.

在之前的文章中,我们介绍了创建Array实例的方法。 其中一些是Public实例方法,有些是Public类方法。 我们还应该知道它们彼此之间有何不同。 现在我们知道了多种方法来声明或生成Array实例。 有些直接绕过某些参数,有些则借助先前定义的Array对象。 现在,我们将学习如何向先前定义的数组添加一些元素? 在本文中,我们将学习<< ,我们可以借助<<将元素添加到Array类的实例。

Method description:

方法说明:

This is a public instance method. As discussed above, this method is used to add elements in a previously declared object of the Array class. This method works in a way that pushes the object to the end of the Array instance which is passed as the parameter to this symbol. This is a destructive method by nature as the changes created by this method are permanent and can't be changed later.

这是一个公共实例方法。 如上所述,此方法用于在Array类的先前声明的对象中添加元素。 此方法的工作方式是将对象推送到Array实例的末尾,该实例作为参数传递给此符号。 本质上,这是一种破坏性方法,因为此方法创建的更改是永久性的,以后无法更改。

Syntax:

句法:

    array_instance << object

Parameter(s):

参数:

This method takes only one parameter which is the instance of Array and it is passed at the left-hand side of the operator or method.

此方法仅使用一个参数(它是Array的实例),并在运算符或方法的左侧传递。

Example 1:

范例1:

=begin
  Ruby program to add an Array to Another 
  with the help of <<
=end

# array declaration
old_arr1 = ['Payal','Samir','Sonakshi','Hira','Panna']

# adding elements
old_arr1 << 'Garvit'
old_arr1 << 'Monika'
old_arr1 << 'Anushree'

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

Output

输出量

The new String Array Instance is:
["Payal", "Samir", "Sonakshi", "Hira", "Panna", "Garvit", "Monika", "Anushree"]

Explanation:

说明:

In the above code, you can observe that we are pushing or adding a String class object at the end of the Array instance which is passed as the parameter to the << operator or method. At the last when we are printing the Array object then you can observe the reflection of that object in the Array instance.

在上面的代码中,您可以观察到我们在Array实例的末尾推入或添加String类对象,该对象作为参数传递给<<操作符或方法 。 最后,当我们打印Array对象时,您可以观察到该对象在Array实例中的反射。

Example 2:

范例2:

=begin
  Ruby program to add an Array to Another 
  with the help of <<
=end

# array declarations
old_arr1 = ['Ramit','Amit','Suresh','Payal']
old_arr2 = ['Payal','Samir','Sonakshi','Hira','Panna']

# adding elements of old_arr2 to old_arr1
old_arr1 << old_arr2

# printing array elements
puts "The new String Array Instance is: "
print old_arr1

Output

输出量

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

Explanation:

说明:

In the above code, you can observe that we are adding or pushing an Array instance to the end of another Array. Now our second Array is residing in the first Array at the last index. So, it can be accessed with the help of the last index only.

在上面的代码中,您可以观察到我们正在将Array实例添加或推入另一个Array的末尾。 现在我们的第二个数组位于最后一个索引的第一个数组中。 因此,只能在最后一个索引的帮助下进行访问。

翻译自: https://www.includehelp.com/ruby/adding-elements-into-an-array-instance-with-left-shift-operator.aspx

ruby三元操作符

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值