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 -> object
    or
    array_instance.sample(n)-> new_array

Argument(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:

=begin
  Ruby 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.sample

puts "The random object generated from Array instance is #{rn}"

Output

输出量

RUN 1:
Array sample implementation
The random object generated from Array instance is 8

RUN 2:
Array sample implementation
The random object generated from Array instance is 18

Explanation:

说明:

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:

=begin
  Ruby 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_i

rn = 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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值