array.slice
Array.slice()方法 (Array.slice() Method)
In this article, we will study about Array.slice() method. You all must be thinking the method must be doing something which is related to the slicing of elements or objects in the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.
在本文中,我們將研究Array.slice()方法 。 你們都必須認為方法必須執行與Array實例中的元素或對象切片有關的操作。 它并不像看起來那么簡單。 好吧,我們將在其余內容中解決這個問題。 我們將嘗試借助語法并演示程序代碼來理解它。
Method description:
方法說明:
This method is a public instance method and defined for the Array class in Ruby's library. This method works on element reference and returns the element at the index which is passed with the method invocation. If you are passing two parameters with the method and those parameters are the start and the length then the method will return a subarray which will contain the elements from the start index and till the length index. This method will return subarray in the case when the range is passed as the parameter at the time of method invocation. This method is one of the examples of the non-destructive method where the method does not bring any change in the actual arrangement of objects in the self Array.
該方法是一個公共實例方法,為Ruby庫中的Array類定義。 此方法在元素引用上起作用,并在與方法調用一起傳遞的索引處返回元素。 如果您通過方法傳遞兩個參數,并且這些參數分別是開始和長度,則該方法將返回一個子數組,該子數組將包含從開始索引到長度索引的元素。 在方法調用時將范圍作為參數傳遞的情況下,此方法將返回子數組。 此方法是非破壞性方法的示例之一,該方法不會對self Array中的對象的實際排列帶來任何改變。
Syntax:
句法:
array_instance.slice(index) -> object or nil
or
array_instance.slice(start,length)-> new_array or nil
or
array_instance.slice(range)-> new_array or nil
Argument(s) required:
所需參數:
You can provide a single index or range or start and length as the argument inside this method at the time of method call. You will get the output on the basis of the argument you pass inside the method.
在方法調用時,可以在此方法內提供單個索引或范圍,起始和長度作為參數。 您將根據在方法內部傳遞的參數獲得輸出。
Example 1:
范例1:
=begin
Ruby program to demonstrate slice method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array slice implementation"
puts "Enter the index you want to slice"
ind = gets.chomp.to_i
if(table.slice(ind))
puts "The element which is sliced is #{table.slice(ind)}"
else
puts "Array index out of bound"
end
puts "Array instance after slicing: #{table}"
Output
輸出量
Array slice implementation
Enter the index you want to slice
4
The element which is sliced is 10
Array instance after slicing: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Explanation:
說明:
In the above code, you can observe that we are slicing the element from the Array instance with the help of Array.slice() method. We are slicing it with the help of an index for which we have asked the user to an input value. The 4th index object has been sliced from the Array instance. The method is not bringing changes in the self Array due to the fact that this method is one of the examples of non-destructive methods.
在上面的代碼中,您可以觀察到我們正在借助Array.slice()方法從Array實例中切片元素 。 我們將在要求用戶輸入輸入值的索引的幫助下對其進行切片。 已從Array實例中切片了第四個索引對象。 由于該方法是非破壞性方法的示例之一,因此該方法未在self Array中帶來任何變化。
Example 2:
范例2:
=begin
Ruby program to demonstrate slice method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array slice implementation"
puts "Enter the start index you want to slice"
ind = gets.chomp.to_i
puts "Enter the length"
len = gets.chomp.to_i
if(table.slice(ind,len))
puts "The sub array which is sliced is #{table.slice(ind,len)}"
else
puts "Array index out of bound"
end
puts "Array instance after slicing: #{table}"
Output
輸出量
Array slice implementation
Enter the start index you want to slice
3
Enter the length
5
The sub array which is sliced is [8, 10, 12, 14, 16]
Array instance after slicing: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Explanation:
說明:
In the above code, you can observe that we are creating a subarray from the elements of the Array instance with the help of Array.slice() method. We are slicing it with the help of two parameters which namely start index and length for which we have asked the user to input values. In the output, you can see that the Array instance has been sliced from the 3rd index and to the 7th index resulting in the formation of an Array which contains five objects. The method is not bringing changes in the self Array due to the fact that this method is one of the examples of the non-destructive methods.
在上面的代碼中,您可以觀察到我們是在Array.slice()方法的幫助下從Array實例的元素創建子數組的 。 我們在兩個參數的幫助下對其進行切片,即我們要求用戶輸入值的起始索引和長度。 在輸出中,你可以看到,Array實例已經從第三索引,并導致其中包含五個對象的陣列的形成的7 個索引切片。 由于該方法是非破壞性方法的示例之一,因此該方法未在self Array中帶來任何變化。
翻譯自: https://www.includehelp.com/ruby/array-slice-method-with-example.aspx
array.slice