ruby array_Ruby中带有示例的Array.sample()方法
ruby array
Array.sample()方法 (Array.sample() Method)
In this article, we will study about Array.sample() method. You all must be thinking the method must be doing something which is quite different from all those methods which we have studied. 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.sample()方法 。 大家都必須認為該方法必須做的事情與我們研究的所有這些方法截然不同。 它并不像看起來那么簡單。 好吧,我們將在其余內容中解決這個問題。 我們將嘗試借助語法并演示程序代碼來理解它。
Method description:
方法說明:
This method is a public instance method and defined for the Array class in Ruby's library. This method works in such a way that it chooses a random object let say r from the elements of Array instance. If you are providing a number 'n' along with the method, then the method will return an Array instance which will be of length 'n' and contains unrepeated and unique random numbers. If the method is invoked without providing any parameter, the method will return 'nil' if the Array instance is found to be empty and an empty Array object is returned if the method is invoked with the argument with the Array which has no elements.
該方法是一個公共實例方法,為Ruby庫中的Array類定義。 此方法的工作方式是,從Array實例的元素中選擇一個隨機對象,例如r。 如果與方法一起提供數字“ n” ,則該方法將返回一個數組實例,該實例的長度為“ n”,并且包含未重復且唯一的隨機數。 如果在沒有提供任何參數的情況下調用該方法,則如果發現Array實例為空,則該方法將返回“ nil”;如果使用帶有不帶任何元素的Array的參數調用該方法,則該方法將返回空Array對象。
Syntax:
句法:
array_instance.sample -> objectorarray_instance.sample(n)-> new_arrayArgument(s) required:
所需參數:
This method takes one argument which decides the length of the Array instance which is returned by the method.
此方法采用一個參數,該參數確定該方法返回的Array實例的長度。
Example 1:
范例1:
=beginRuby program to demonstrate sample method =end# array declaration table = [2,4,6,8,10,12,14,16,18,20]puts "Array sample implementation" rn = table.sampleputs "The random object generated from Array instance is #{rn}"Output
輸出量
RUN 1: Array sample implementation The random object generated from Array instance is 8RUN 2: Array sample implementation The random object generated from Array instance is 18Explanation:
說明:
In the above code, you can observe that we are generating random numbers from the Array instance with the help of Array.sample() method. In both the Runs, you can observe that the random numbers being generated are totally different from each other.
在上面的代碼中,您可以觀察到借助于Array.sample()方法 ,我們正在從Array實例生成隨機數。 在兩次運行中,您都可以觀察到生成的隨機數彼此完全不同。
Example 2:
范例2:
=beginRuby program to demonstrate sample method =end# array declaration table = [2,4,6,8,10,12,14,16,18,20]puts "Array sample implementation"puts "Enter the number of objects you want to generate:" num = gets.chomp.to_irn = table.sample(num)puts "The random objects generated from Array instance is #{rn}"Output
輸出量
RUN 1: Array sample implementation Enter the number of objects you want to generate:3 The random objects generated from Array instance is [20, 18, 2]RUN 2: Array sample implementation Enter the number of objects you want to generate:3 The random objects generated from Array instance is [2, 6, 8]Explanation:
說明:
In the above code, you can observe that we are generating an Array instance of Random numbers from the Array instance with which Array.sample() method has been invoked. In both the Runs, you can see that the Array instances obtained are totally different from each other.
在上面的代碼中,您可以觀察到我們正在從調用Array.sample()方法的Array實例生成隨機數的Array實例。 在這兩次運行中,您都可以看到獲得的Array實例彼此完全不同。
翻譯自: https://www.includehelp.com/ruby/array-sample-method-with-example.aspx
ruby array
總結
以上是生活随笔為你收集整理的ruby array_Ruby中带有示例的Array.sample()方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python函数实例化_用Python实
- 下一篇: java方法重载和重载方法_我们可以在J