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 << objectParameter(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:
=beginRuby 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_arr1Output
輸出量
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:
=beginRuby 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_arr1Output
輸出量
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三元操作符
總結
以上是生活随笔為你收集整理的ruby三元操作符_在Ruby中使用操作符将元素添加到数组实例中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java PushbackInputSt
- 下一篇: python函数示例_带Python示例